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 operation on arrays with nils

For general discussion related FlowStone

ruby operation on arrays with nils

Postby tester » Tue Jan 19, 2016 9:53 pm

Scenario. Two input arrays, that on gui level contain empty lines. Nil lines are not zero's in this case. How to perform math operations on two such arrays in a way, that nil input (in one or other aray) produces nil output?

Example:

array1 + array2 = array3

1 + 5 = 6
nil + 6 = nil
2 + nil = nil
0 + 8 = 8
3 + 9 = 12

I'm guessing it would have to accept string array inputs and string array output.
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
tester
 
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: ruby operation on arrays with nils

Postby tulamide » Tue Jan 19, 2016 11:34 pm

Code: Select all
a = 1, nil, 2, 0, 3
b = 5, 6, nil, 8, 9

c = a.zip(b).map {|x, y| if x == nil or y == nil then nil else x+y end}
# c is [6, nil, nil, 8, 12]
"There lies the dog buried" (German saying translated literally)
tulamide
 
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: ruby operation on arrays with nils

Postby tester » Wed Jan 20, 2016 10:12 am

Not exactly working.

If inputs are set to string arrays, then values are connected, like 1+5=15. If inputs are float arrays, then nil is converted to 0 before it comes to ruby. Plus - the if statement seems to be ignored anyway. So input string values must be converted within ruby window to floats (but with nils different from 0's), before processing via math.
Attachments
ruby-nils.fsm
(485 Bytes) Downloaded 777 times
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
tester
 
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: ruby operation on arrays with nils

Postby tulamide » Wed Jan 20, 2016 1:18 pm

It wasn't clear from your description, that the arrays would be green ones. A good example of why a schematic can yield better help results ;)

For feeding green arrays into Ruby in the way you need them, only strings make sense. You'd then have to change the algorithm to
Code: Select all
@a.zip(@b).map {|x, y| if x == "" or y == "" then "" else x.to_f+y.to_f end}


See also attached fsm.
Attachments
ruby-nils[tula].fsm
(417 Bytes) Downloaded 829 times
"There lies the dog buried" (German saying translated literally)
tulamide
 
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: ruby operation on arrays with nils

Postby tester » Wed Jan 20, 2016 5:45 pm

Thanks, this one works.

Not on all machines I have the FS installed.
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
tester
 
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet


Return to General

Who is online

Users browsing this forum: No registered users and 73 guests