From: James Edward Gray II Date: 2006-01-05T04:07:52+09:00 Subject: Re: Question on iterating a hash On Jan 4, 2006, at 12:46 PM, phil swenson wrote: > I have a hash that looks like this : {"1"=>"0, "3"="1", "45"=>"1", > "101"=>"0"} > > What I want is an array of all the keys that have hash values equal to > "1" ["3","45"] in this case. > > Yeah, i can just iterate the keys... but it seems like there is a > "ruby" > way to do this. Any thoughts? >> h = Hash[*%w{1 0 3 1 45 1 101 0}] => {"45"=>"1", "1"=>"0", "101"=>"0", "3"=>"1"} >> h.select { |key, value| value == "1" }.map { |key, value| key } => ["45", "3"] Hope that helps. James Edward Gray II