Hello,
Sorry for the confusion! Thanks for pointing this out Fredo6. I'll try to better explain my predicament...
I have list2 (beaphash and spihash - a list of all hashes) and list1 (user-generated list where each of the elements refers to a hash). So, I have a user-generated list (this code is just a snippet) and I want to access the the hashes that correspond to list1 and pull, say, the length of the "companion" array from these hashes.
beaphash = Hash["companion" => %w{carrot corn pea}, "antagonist" => %w{beet broccoli onion}, "name" => "bean(pole)"]
spihash = {"companion" => %w{lettuce pea radish}, "antagonist" => %w{}, "name" => "spinach"}
tomhash = {"companion"=> %w{bean(pole) potato}, "antagonist" => %w{carrot}, "name"=> "tomato"}
list1 = %w{bean(pole) tomato}
list2 = %w{beaphash spihash tomhash}
list3 = Array.new
list1.each do|x|
list2.each do|y|
if y["name"] == x
list3 << (y["companion"]).length
puts "Hey, hash length; #{y.length} and name from hash; #{y["name"]} and name from list1; #{x}"
puts "But beaphash name is #{beaphash["name"]}"
else
end
end
end
...then I can interleave list1 and list3 to make a new hash.
I realize that I may have made this more complicated than it needs to be and there may be a simpler solution...I'm really new to Ruby!
Any help would be very much appreciated!
Thanks,
laura