Page 1 of 1

Array hashes in Ruby?

Posted: Wed Sep 21, 2016 12:04 pm
by kortezzzz
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 :)

Re: Array hashes in Ruby?

Posted: Wed Sep 21, 2016 12:22 pm
by KG_is_back

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.

Re: Array hashes in Ruby?

Posted: Wed Sep 21, 2016 1:02 pm
by kortezzzz
Brilliant KG :D
Thank you so much. So far, works perfectly with no issue :)