Support

If you have a problem or need to report a bug please email : support@dsprobotics.com

There are 3 sections to this support area:

DOWNLOADS: access to product manuals, support files and drivers

HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects

USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here

NEW REGISTRATIONS - please contact us if you wish to register on the forum

Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright

Ruby - Adding array indexes of one arryay with other one

For general discussion related FlowStone

Ruby - Adding array indexes of one arryay with other one

Postby kortezzzz » Wed Mar 03, 2021 11:24 pm

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

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

Postby adamszabo » Wed Mar 03, 2021 11:40 pm

yes

Code: Select all
[[1.5, 2, 3.3, 6.4],[1.5, 2.3, 1, 2.4]].transpose.map{|a| a.sum}
adamszabo
 
Posts: 667
Joined: Sun Jul 11, 2010 7:21 am

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

Postby kortezzzz » Wed Mar 03, 2021 11:52 pm

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

Postby kortezzzz » Thu Mar 04, 2021 12:12 am

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

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

Postby adamszabo » Thu Mar 04, 2021 12:23 am

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

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

Postby kortezzzz » Thu Mar 04, 2021 12:32 am

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

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

Postby DaveyBoy » Thu Mar 04, 2021 1:49 am

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]

:)
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

Postby tulamide » Thu Mar 04, 2021 2:32 am

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)
tulamide
 
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

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

Postby kortezzzz » Thu Mar 04, 2021 8:55 am

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


Return to General

Who is online

Users browsing this forum: Google [Bot] and 78 guests