Hello all,
I have a number of hashes to store info for me, they all have the same keys but each has a different value for each key. I've made a user interface which enables a user to input a list of strings corresponding to a value in the hash. Because the list is input by the user, it is important for me to have the ability to iterate over the hashes (say, from a list of the hash names) without having to call each one in an "if" statement every time I need to do an operation (one could imagine how tedious and inefficient it has been to call all my hashes individually ).
I thought something like this...
` a={:name => "eh", :word=> %w{apple, ascend}}
b={:name => "bee", :word=> %w{ball}}
c={:name => "sea", :word=> %w{}}
d={:name => "d", :word => %w{door dog down}}
word_size_array = Array.new
user_specified_list = ["a", "c"]
array_of_hash_names = %w{a b c d}
user_specified_list.each do |x|
array_of_hash_names.each |y|
if y[:name] == x
word_size_array << y[:word].length
end
end`
would have worked but it doesn't.
Instead, I have to do something like this...
user_specified_list.each do |x| if a[:name] == x word_size_array << a[:word].length elsif b[:name] == x word_size_array << b[:word].length elsif c[:name] == x word_size_array << c[:word].length elsif d[:name] == x word_size_array << d[:word].length else end end
...for about 15 (and counting) hashes!
I would like to be able to call the hash using a ".each" or "do" loop but I have not been able to find the trick that would allow me to do this. Does someone have a way to do this (or an easier way to do this database-type stuff;) ?)
All input is welcome,
Thanks,
laura