Ruby - Adding array indexes of one arryay with other one

For general discussion related FlowStone
Post Reply
User avatar
kortezzzz
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

Ruby - Adding array indexes of one arryay with other one

Post 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?
adamszabo
Posts: 667
Joined: Sun Jul 11, 2010 7:21 am

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

Post 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}
User avatar
kortezzzz
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

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

Post 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.
User avatar
kortezzzz
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

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

Post 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!
adamszabo
Posts: 667
Joined: Sun Jul 11, 2010 7:21 am

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

Post 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}
User avatar
kortezzzz
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

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

Post 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 :)
User avatar
DaveyBoy
Posts: 131
Joined: Wed May 11, 2016 9:18 pm
Location: Leeds UK

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

Post 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]

:)
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

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

Post 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)
"There lies the dog buried" (German saying translated literally)
User avatar
kortezzzz
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

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

Post 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.
Post Reply