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

Array hashes in Ruby?

For general discussion related FlowStone

Array hashes in Ruby?

Postby kortezzzz » Wed Sep 21, 2016 12:04 pm

Hi guys,

Here is a little ruby challenge I'm trying to deal with:

I would like to use hashes to define string values just like the "if then" primitive does, but unlike with the standard ruby hashes code where the whole defining process done inside the ruby primitive, I would like to define it externally, by feeding the code with 2 random string arrays. The first array would be used as a "variable to be compared" list and the second would be used as a "condition" list.

So, the ruby code should have 3 inputs and one output. The inputs would be:
1) "user" input
2) "variable to be compared" input
3) "condition" input

The output would be just the "final result".

Each index in the first array is actually equivalent to the same index int second array, so for instance if the arrays are:

a
b
c

1
2
3

it means that a=1,b=2,c=3. Therefore, if the user's string input is "cab" the array would 3,1,2 so the "final result" would be:

3
1
2

Also attached a small diagram schematic that describes the wanted structure.
Thanks :)
Attachments
(ruby arrays if then).fsm
(502 Bytes) Downloaded 832 times
User avatar
kortezzzz
 
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

Re: Array hashes in Ruby?

Postby KG_is_back » Wed Sep 21, 2016 12:22 pm

Code: Select all
h=Hash.new
@ins[1].zip(@ins[2]).each{|(k,v)| h[k]=v}

output 0,@ins[0].map{|k| h[k]}


Note: the code is not idiot-proof. If the "hash arrays" are not of equal size, errors may occur.
KG_is_back
 
Posts: 1196
Joined: Tue Oct 22, 2013 5:43 pm
Location: Slovakia

Re: Array hashes in Ruby?

Postby kortezzzz » Wed Sep 21, 2016 1:02 pm

Brilliant KG :D
Thank you so much. So far, works perfectly with no issue :)
User avatar
kortezzzz
 
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm


Return to General

Who is online

Users browsing this forum: No registered users and 33 guests