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

Add to int array with ruby; how?

For general discussion related FlowStone

Add to int array with ruby; how?

Postby kortezzzz » Sun Mar 04, 2018 2:40 pm

Hi all. A ruby question:
Seems like we have to use ruby to add each int value in a int array with a fixed number, because there is no "add to int array" primitive. lets say I have A given array like

3
4
2
3

and I would like to add 2 to each value and end up with

5
6
4
5

How it done with ruby, by using 2 inputs (one input for the array and one for the int value that added to each value in the array) and one int array output for the results?
User avatar
kortezzzz
 
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

Re: Add to int array with ruby; how?

Postby KG_is_back » Sun Mar 04, 2018 2:45 pm

assuming first input is the array and second input is the integer

Code: Select all
output @ins[0].map!{|v| v+@ins[1]}
KG_is_back
 
Posts: 1196
Joined: Tue Oct 22, 2013 5:43 pm
Location: Slovakia

Re: Add to int array with ruby; how?

Postby tulamide » Sun Mar 04, 2018 2:55 pm

I wouldn't use the altering version. Just "map" without "!" will do. You're outputting an array anyway so why bothering with the slower alteration of the original array?
"There lies the dog buried" (German saying translated literally)
tulamide
 
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Add to int array with ruby; how?

Postby KG_is_back » Sun Mar 04, 2018 3:21 pm

tulamide wrote:I wouldn't use the altering version. Just "map" without "!" will do. You're outputting an array anyway so why bothering with the slower alteration of the original array?


the ! is in fact faster, because it does not allocate a second array - it just modifies the existing one element by element. The inputs and outputs inside ruby actually aren't directly usable by flowstone - flowstone converts them to green connectors on the way out and vice versa for the inputs. Using the ! version ensures that internally, the same array is in the @ins array and @outs array, saving memory and CPU. Although very marginally...
KG_is_back
 
Posts: 1196
Joined: Tue Oct 22, 2013 5:43 pm
Location: Slovakia

Re: Add to int array with ruby; how?

Postby tulamide » Sun Mar 04, 2018 3:42 pm

KG_is_back wrote:
tulamide wrote:I wouldn't use the altering version. Just "map" without "!" will do. You're outputting an array anyway so why bothering with the slower alteration of the original array?


the ! is in fact faster, because it does not allocate a second array - it just modifies the existing one element by element. The inputs and outputs inside ruby actually aren't directly usable by flowstone - flowstone converts them to green connectors on the way out and vice versa for the inputs. Using the ! version ensures that internally, the same array is in the @ins array and @outs array, saving memory and CPU. Although very marginally...

You're right! Since Flowstone doesn't allow for long iterations, I looked on the internet instead. Here is one example, where "merge" is used in both forms (I just learned the ! ones are called the bang! versions). Time is in seconds, array length is 10000, ten consecutive runs
Code: Select all
                 user     system      total        real
merge!       0.010000   0.000000   0.010000 (  0.011370)
merge       17.710000   0.000000  17.710000 ( 17.840856)
"There lies the dog buried" (German saying translated literally)
tulamide
 
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Add to int array with ruby; how?

Postby kortezzzz » Sun Mar 04, 2018 4:15 pm

Thanks KJ, but this ime tula's "no !" worked as expected :)

For some reason, when you add the "!" to the code, it saves older values and then add them with the new ones.
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 80 guests