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?
thx
phil
···
--
Posted via http://www.ruby-forum.com/.
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
···
On Jan 4, 2006, at 12:46 PM, phil swenson wrote:
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?
thx
phil
{"1"=>"0", "3"=>"1", "45"=>"1","101"=>"0"}.select{|k,v| v=="1"}.map{|e|e.first}
lopex
Another option:
h.map{|k,v| k if v=='1'}.compact
And of course an inject version:
h.inject(){|a,b| a<<b[0] if b[1]=='1';a}
Ryan
···
On 1/4/06, phil swenson <phil.swenson@gmail.com> wrote:
Yeah, i can just iterate the keys... but it seems like there is a "ruby"
way to do this. Any thoughts?
Wouldn't it be easier to iterate through the keys? I'm not sure why
Phil didn't want to. (Phil?)
h.keys.select {|k| h[k] == 1}
Since he wants the keys of the hash in an array, wouldn't you need to do
this..
result = h.keys.select {|k| h[k] == 1}
result = result.keys
I'm sure there's a one-liner for this somewhere, but I need more coffee.
···
--
Posted via http://www.ruby-forum.com/\.
dblack@wobblini.net wrote:
Hi --
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"]
Wouldn't it be easier to iterate through the keys? I'm not sure why
Phil didn't want to. (Phil?)
h.keys.select {|k| h[k] == 1}
David
Don't know, but here is another one:
h.select{|k, v| v == "1" }.transpose.first
cheers
Simon
···
On Thu, 5 Jan 2006, James Edward Gray II wrote:
On Jan 4, 2006, at 12:46 PM, phil swenson wrote:
True. But I'd love to see a real-world example of someone doing that.
Sounds like code-smell to me.
Ryan
···
On 1/4/06, dblack@wobblini.net <dblack@wobblini.net> wrote:
If you're sure nil isn't a key
No. h.keys is an array, and select returns an array.
David
···
On Thu, 5 Jan 2006, Kevin Olbrich wrote:
Wouldn't it be easier to iterate through the keys? I'm not sure why
Phil didn't want to. (Phil?)
h.keys.select {|k| h[k] == 1}
Since he wants the keys of the hash in an array, wouldn't you need to do
this..
result = h.keys.select {|k| h[k] == 1}
result = result.keys
--
David A. Black
dblack@wobblini.net
"Ruby for Rails", from Manning Publications, coming April 2006!
I'd be very suprised if it ever happened. But I still like the simple
select version best
David
···
On Thu, 5 Jan 2006, Ryan Leavengood wrote:
On 1/4/06, dblack@wobblini.net <dblack@wobblini.net> wrote:
If you're sure nil isn't a key
True. But I'd love to see a real-world example of someone doing that.
Sounds like code-smell to me.
--
David A. Black
dblack@wobblini.net
"Ruby for Rails", from Manning Publications, coming April 2006!
i think it's more :
h.keys.select {|k| h[k] == "1"}
if h is really :
h = Hash[*%w{1 0 3 1 45 1 101 0}]
that's to say String not Fixnum ))
···
Kevin Olbrich <kevin.olbrich@duke.edu> wrote:
result = h.keys.select {|k| h[k] == 1}
--
une bévue