How to sum elements from different lists

Roger Vilà's icon

Hello,

I want to sum the elements from 2 lists, and I want to know which is the best way.
For example, imagine the first list is 1 9 2 and the second one is 8 4 6, so I need to sum 1+8, 9+4, 2+6.
There is no zl object to do this. I have tried to create a JavaScript object but it has problems while reading lists.

Thank you very much.

pdelges's icon

Have a look at the vexpr object.

Evan's icon

The list stuff was tricky (kind of, you can't just take a list x, and output it as a list). I don't know if there's a built in method for doing what I did here:

autowatch = 1;
inlets = 2;
outlets = 1;

var list_1 = [];
var list_2 = [];
var output = [];

function list(x){
    if(inlet == 0){
        for(i = 0; i < arguments.length; i ++){
            list_1[i] = arguments[i];
        }
        for(i = 0; i < arguments.length; i++){
            output[i] = list_1[i]+ list_2[i]; 
        }            
        outlet(0, output);
    }
    if(inlet == 1){
        for(i = 0; i < arguments.length; i ++){
            list_2[i] = arguments[i];
        }
    }
}

But this seems to work.

EDIT: also, the vexpr object ;)

florian1947's icon
Max Patch
Copy patch and select New From Clipboard in Max.

Why not the simple way?

Roger Vilà's icon

@Patrick Delges thank you! I knew expr but not vexpr. It worked perfectly

@evan thank you, working with lists with JavaScript in Max seems to be a little messy. The max js documentation is not 100% clear. Do you know if there is a book focused on js for max?

@florian1947 thank you, but the Idea was to work with dynamic lists, so pack/unpack is not useful in this case.

Thank you very much for your help!

Stephane Morisse's icon
Max Patch
Copy patch and select New From Clipboard in Max.

For the fun of zl objects, got this one without vexpr. And the lists can have variable lengths.

Roger Vilà's icon

@Stephane Morisse That one is cool! Is it better to use vexpr in terms of performance and RAM use? I am trying to optimize my patch.
Thank you!

Stephane Morisse's icon

TBH, I've got no idea. Vexpr is by far the simpler one-object solution but you've got to unpack the result and if you want to deal with variable lengths list, you'll have to work out a solution with another object than unpack.