How to multiply 2 constant list then sum all the values?

Can someone please help me, i need to multiply 2 constant lists then sum all the results
for example
Const list 1 contains
5
6
3

Const list 2 contains
4
7
8
what i need is
5+4=9
6+7=13
8+3=11
then sum all the results
9+13+11=33

Note: the number of values aren’t always 3 in a list
thanks.

Given these 2 lists

BLOCK:ConstantList
  value = ["5", "6", "3"]
  => VAR @list1
ENDBLOCK

BLOCK:ConstantList
  value = ["4", "7", "8"]
  => VAR @list2
ENDBLOCK

Shitty LoliCode solution

BLOCK:ZipLists
  list1 = @list1
  list2 = @list2
  format = "[0]+[1]"
  => VAR @zipped
ENDBLOCK

for (int i = 0; i < zipped.Count; i++)
{
BLOCK:Compute
  input = @zipped[i]
  => VAR @result
ENDBLOCK

zipped[i] = result.AsString();
}

BLOCK:JoinList
  list = @zipped
  separator = "+"
  => VAR @joined
ENDBLOCK

BLOCK:Compute
  input = @joined
  => VAR @sum
ENDBLOCK

Better full C# Linq solution

int sum = list1.Select((e, i) => int.Parse(e) + int.Parse(list2[i])).Sum();

Anyways, since you are basically summing all elements (the order of operations doesn’t matter since you’re just doing additions) why don’t you just concatenate the two lists and sum them together?

int sum = list1.Concat(list2).Select(e => int.Parse(e)).Sum();

It gets you the exact same result (assuming you’re working with numbers and not strings).

that was fast!, thank you so much, i just noticed i typed something wrong
i needed to multiply then sum
like this
5 * 4=20
6 * 7=42
8 * 3=24
anyway i used the lolicode and changed + to * in ziplist block and it worked
thanks again.

1 Like