r/learnruby • u/ikkitousendestiny • Oct 29 '15
How to find value in a giant hash?
I've been struggling with this all day. So please help if you can. I need to confirm that a value exists deep inside a hash. But the values in the hash can be other hashes or arrays, and the key may repeat. The closest thing I've been able to find is this link:
But none of the answers there search beyond the first instance of the key.
So if I had something like this: hash = {"items"=>[{"name"=>"bob", "account-id"=>"1", "message"=>nil, "updated-at"=>"2015-10-28T18:58:38Z", "created-at"=>"2015-10-28T18:58:38Z", "status"=>"error"}, {"name"=>"john", "account-id"=>"2", "message"=>nil, "updated-at"=>"2015-10-29T13:25:04Z", "created-at"=>"2015-10-29T13:25:03Z", "status"=>"error"}, {"name"=>"larry", "account-id"=>"3", "message"=>nil, "updated-at"=>"2015-10-28T15:40:30Z", "created-at"=>"2015-10-28T15:40:30Z", "status"=>"error", "type"=>"export"}
And I want to see if there was a "name" => larry, it'd return true, or the name itself, or a list of all the names. None of the options I've found seem to be able to search for larry and the ones in that link will only return the first "name".
I'm really stumped.
1
u/ikkitousendestiny Oct 29 '15
I keep getting stuck on the recursive part I guess. No matter what I try it never finds the value. Or if it finds the value it gets stuck if I add an array somewhere.
1
u/mr_patsy Oct 29 '15
Recursion will work here , but it's not necessary if you are just trying to learn how to mess about with hashes. Think about what you want to do here: find a value within a hash. This hash is part of a larger hash with values that may require further digging (they are either values or arrays). So start your block with:
Now you're iterating through the main hash. Does my value equal Larry? If yes, return directly out. If no, you need to explore that some more. Experiment a bit and you'll get it. Then when you have the iterative method sketched out, try doing it recursively like /u/kenhkelly is suggesting.
1
u/kenhkelly Oct 29 '15
On my phone, but it seems like a recursion function could work for you. Pass in the value and loop through it. If the value is an array, run it again passing in that array, repeat until you find your value.