Page 1 of 1

Ruby - Adding array indexes of one arryay with other one

PostPosted: Wed Mar 03, 2021 11:24 pm
by kortezzzz
Hi,
Wondered if there is any elegant Ruby shortcut to add float array indexes with other float array indexes while keeping their correct order.

For example, let's say we have this array:

1.5
2
3.3
6.4

And we want to add it this array:

1.5
2.3
1
2.4

To get this results at the output:

3
4.3
4.3
8.8

Possible?

Re: Ruby - Adding array indexes of one arryay with other one

PostPosted: Wed Mar 03, 2021 11:40 pm
by adamszabo
yes

Code: Select all
[[1.5, 2, 3.3, 6.4],[1.5, 2.3, 1, 2.4]].transpose.map{|a| a.sum}

Re: Ruby - Adding array indexes of one arryay with other one

PostPosted: Wed Mar 03, 2021 11:52 pm
by kortezzzz
Thank you Adam,

That's great solution when your variables are fixed and not changeable. Is there any way to do it without specifying the exact parameters? I mean just to tell Ruby to add together similar indexes of 2 given arrays.

Re: Ruby - Adding array indexes of one arryay with other one

PostPosted: Thu Mar 04, 2021 12:12 am
by kortezzzz
Well, I've reproduced it according to your example and it works :)

The exact code syntax is (assuming that the first array is @a, the second array is @b and the output is @c):

Code: Select all
@c=[@a,@b].transpose.map{|a| a.sum}
output 0, @c


Thanks again!

Re: Ruby - Adding array indexes of one arryay with other one

PostPosted: Thu Mar 04, 2021 12:23 am
by adamszabo
Yes I forgot to mention that in my example I just put the same values that you did in your example, but you can put any values there, but I see you figured it out ;)

But if you are just using a single ruby to send this out, you dont need the @c variable you can just write:

Code: Select all
output [@a,@b].transpose.map{|a| a.sum}

Re: Ruby - Adding array indexes of one arryay with other one

PostPosted: Thu Mar 04, 2021 12:32 am
by kortezzzz
adamszabo wrote:Yes I forgot to mention that in my example I just put the same values that you did in your example, but you can put any values there, but I see you figured it out ;)

But if you are just using a single ruby to send this out, you dont need the @c variable you can just write:

Code: Select all
output [@a,@b].transpose.map{|a| a.sum}


Yes, indeed :)

Re: Ruby - Adding array indexes of one arryay with other one

PostPosted: Thu Mar 04, 2021 1:49 am
by DaveyBoy
Also you are not limited to two arrays, as long as they all are the same size:

[[1.5, 2, 3.3, 6.4],[1.5, 2.3, 1, 2.4],[10,10,10,10]].transpose.map{|a| a.sum}

=> [13.0, 14.3, 14.3, 18.8]

:)

Re: Ruby - Adding array indexes of one arryay with other one

PostPosted: Thu Mar 04, 2021 2:32 am
by tulamide
Another way is zip() and reduce():
Code: Select all
a = [1, 2, 3, 4]
b = [5, 6, 7, 8]

a.zip(b).map {|n| n.reduce(:+)}


Works with any number of arrays as well:
Code: Select all
a = [1, 2, 3, 4]
b = [5, 6, 7, 8]
c = [9, 10, 11, 12]

a.zip(b, c).map {|n| n.reduce(:+)}


This is what you HAVE to use in Flowstone 3, because it runs Ruby 1.9.3 and that Ruby version doesn't know sum() for arrays.

(Remember, FS 4 related stuff only in the slack group)

Re: Ruby - Adding array indexes of one arryay with other one

PostPosted: Thu Mar 04, 2021 8:55 am
by kortezzzz
Great stuff guys 8-) Thank you.
I'm collecting all this impish Ruby shortcuts and I'll probably post them as a Ruby expansion pack for newbies once the FS4 becomes official.