[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Question on iterating a hash

phil swenson

1/4/2006 6:46:00 PM

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


15 Answers

Marcin Mielzynski

1/4/2006 7:04:00 PM

0

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

James Gray

1/4/2006 7:08:00 PM

0

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


dblack

1/4/2006 7:27:00 PM

0

dblack

1/4/2006 7:36:00 PM

0

Kevin Olbrich

1/4/2006 7:52:00 PM

0

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


Simon Kröger

1/4/2006 7:54:00 PM

0

dblack@wobblini.net wrote:

> Hi --
>
> On Thu, 5 Jan 2006, James Edward Gray II wrote:
>
>> 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"]
>
>
> 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




dblack

1/4/2006 8:15:00 PM

0

Kevin Olbrich

1/4/2006 8:21:00 PM

0

unknown wrote:
> On Thu, 5 Jan 2006, Kevin Olbrich wrote:
>
>> result = result.keys
> No. h.keys is an array, and select returns an array.
>

See, I told you I needed more coffee.

--
Posted via http://www.ruby-....


dblack

1/4/2006 8:33:00 PM

0

Ryan Leavengood

1/4/2006 9:22:00 PM

0

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?

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