Question on iterating a hash

P

phil swenson

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
 
M

Marcin Mielżyński

phil said:
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
 
J

James Edward Gray II

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
 
D

dblack

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

--
David A. Black
(e-mail address removed)

"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.manning.com/books/black
 
K

Kevin Olbrich

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.
 
S

Simon Kröger

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
 
D

dblack

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

No. h.keys is an array, and select returns an array.


David

--
David A. Black
(e-mail address removed)

"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.manning.com/books/black
 
R

Ryan Leavengood

Yeah, i can just iterate the keys... but it seems like there is a "ruby"
way to do this. Any thoughts?

Another option:

h.map{|k,v| k if v=3D=3D'1'}.compact

And of course an inject version:
h.inject([]){|a,b| a<<b[0] if b[1]=3D=3D'1';a}

Ryan
 
D

dblack

True. But I'd love to see a real-world example of someone doing that.
Sounds like code-smell to me.

I'd be very suprised if it ever happened. But I still like the simple
select version best :)


David

--
David A. Black
(e-mail address removed)

"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.manning.com/books/black
 
R

Robert Klemme

Ryan said:
Yeah, i can just iterate the keys... but it seems like there is a
"ruby" way to do this. Any thoughts?

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}

You can even access key and value separately:

h.inject([]) {|a,(k,v)| a << k if v == '1'; a}

:)

robert
 
U

Une bévue

Kevin Olbrich said:
result = h.keys.select {|k| h[k] == 1}

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 ))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top