Forum: Ruby Question on iterating a hash

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1ed4bbef573bfc014d32356d53103ca2?d=identicon&s=25 phil swenson (Guest)
on 2006-01-04 19:45
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
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-01-04 20:09
(Received via mailing list)
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
B97225f66bb5caac601b12735d430a0d?d=identicon&s=25 =?UTF-8?B?TWFyY2luIE1pZWzFvHnFhHNraQ==?= (Guest)
on 2006-01-04 20:09
(Received via mailing list)
phil swenson wrote:
> phil
>

{"1"=>"0", "3"=>"1", "45"=>"1","101"=>"0"}.select{|k,v|
v=="1"}.map{|e|e.first}

lopex
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-01-04 20:27
(Received via mailing list)
Hi --

On Thu, 5 Jan 2006, James Edward Gray II wrote:

>
>>> 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
dblack@wobblini.net

"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.manning.com/books/black
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-01-04 20:36
(Received via mailing list)
On Thu, 5 Jan 2006, dblack@wobblini.net wrote:

>  h.keys.select {|k| h[k] == 1}

s/1/"1"/ :-)

--
David A. Black
dblack@wobblini.net

"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.manning.com/books/black
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-01-04 20:51
> 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.
A9c4658e9e475e13d790ae419acf01b6?d=identicon&s=25 =?ISO-8859-1?Q?Simon_Kr=F6ger?= (Guest)
on 2006-01-04 20:54
(Received via mailing list)
dblack@wobblini.net wrote:

>>> "1" ["3","45"] in this case.
>>
>> => ["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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-01-04 21:15
(Received via mailing list)
On Thu, 5 Jan 2006, Kevin Olbrich wrote:

> result = result.keys
No.  h.keys is an array, and select returns an array.


David

--
David A. Black
dblack@wobblini.net

"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.manning.com/books/black
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-01-04 21:20
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.
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-01-04 21:33
(Received via mailing list)
Hi --

On Thu, 5 Jan 2006, Kevin Olbrich wrote:

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

I know the feeling well.  That's why we have multiple people on the
list -- to take up the caffeine slack for each other :-)


David

--
David A. Black
dblack@wobblini.net

"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.manning.com/books/black
4b174722d1b1a4bbd9672e1ab50c30a9?d=identicon&s=25 Ryan Leavengood (Guest)
on 2006-01-04 22:25
(Received via mailing list)
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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-01-04 23:05
(Received via mailing list)
Hi --

On Thu, 5 Jan 2006, Ryan Leavengood wrote:

> 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

If you're sure nil isn't a key :-)


David

--
David A. Black
dblack@wobblini.net

"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.manning.com/books/black
4b174722d1b1a4bbd9672e1ab50c30a9?d=identicon&s=25 Ryan Leavengood (Guest)
on 2006-01-04 23:17
(Received via mailing list)
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.

Ryan
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-01-04 23:59
(Received via mailing list)
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.

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


David

--
David A. Black
dblack@wobblini.net

"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.manning.com/books/black
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 Robert Klemme (Guest)
on 2006-01-05 09:23
(Received via mailing list)
Ryan Leavengood wrote:
> 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
Fa0d14a176f060c96eef797fa48e6bcc?d=identicon&s=25 unknown (Guest)
on 2006-01-05 09:44
(Received via mailing list)
Kevin Olbrich <kevin.olbrich@duke.edu> wrote:

> 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 ))
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-01-05 17:43
(Received via mailing list)
Hi --

On Thu, 5 Jan 2006, Simon Kröger wrote:

>>>>
>>>
>>
>> David
>
> Don't know, but here is another one:
>
> h.select{|k, v| v == "1" }.transpose.first

OK... but I'm still not getting why just "select"ing the ones you want
isn't the best way.


David

--
David A. Black
dblack@wobblini.net

"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.manning.com/books/black
This topic is locked and can not be replied to.