[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Subselecting hash back into hash (oneliner?

Jon Baer

1/26/2006 8:00:00 PM

Going through pick-axe and was just wondering if there was a one
liner for something such as this that I may have missed:

h2 = Hash.new
h1 = {"foo1"=>"bar1", "foo2"=>"bar2"}
query = h1.select { |k,v| v == "bar2" }
query.each do |result|
h2[result[0]] = result[1]
end
p h2

Thanks.

- Jon


5 Answers

James Gray

1/26/2006 8:06:00 PM

0

On Jan 26, 2006, at 1:59 PM, Jon Baer wrote:

> Going through pick-axe and was just wondering if there was a one
> liner for something such as this that I may have missed:
>
> h2 = Hash.new
> h1 = {"foo1"=>"bar1", "foo2"=>"bar2"}
> query = h1.select { |k,v| v == "bar2" }
> query.each do |result|
> h2[result[0]] = result[1]
> end
> p h2

Like this?

>> h1 = {"foo1"=>"bar1", "foo2"=>"bar2"}
=> {"foo1"=>"bar1", "foo2"=>"bar2"}
>> h2 = Hash[*h1.select { |k, v| v == "bar2" }.flatten]
=> {"foo2"=>"bar2"}

Hope that helps.

James Edward Gray II


Jon Baer

1/26/2006 10:56:00 PM

0

Nice .. well that works :-) but in the constructor can you tell me
what "*" would literally "mean"?

Does this just take only an Array obj or could you place any object
in there w/ public attributes/methods?

(Was this covered in pick-axe somewhere I missed?)

Thank you, much appreciated ...

- Jon

On Jan 26, 2006, at 3:06 PM, James Edward Gray II wrote:

> >> h1 = {"foo1"=>"bar1", "foo2"=>"bar2"}
> => {"foo1"=>"bar1", "foo2"=>"bar2"}
> >> h2 = Hash[*h1.select { |k, v| v == "bar2" }.flatten]



Ezra Zygmuntowicz

1/26/2006 11:11:00 PM

0


On Jan 26, 2006, at 2:55 PM, Jon Baer wrote:

> Nice .. well that works :-) but in the constructor can you tell me
> what "*" would literally "mean"?
>
> Does this just take only an Array obj or could you place any object
> in there w/ public attributes/methods?
>
> (Was this covered in pick-axe somewhere I missed?)
>
> Thank you, much appreciated ...
>
> - Jon
>
> On Jan 26, 2006, at 3:06 PM, James Edward Gray II wrote:
>
>> >> h1 = {"foo1"=>"bar1", "foo2"=>"bar2"}
>> => {"foo1"=>"bar1", "foo2"=>"bar2"}
>> >> h2 = Hash[*h1.select { |k, v| v == "bar2" }.flatten]
>
>

The "*" is sometimes called the "splat" operator. If you use the Hash
[] constructor and pass in an array with the * before it it will turn
the array into a hash using pairs of the array members to create the
key => values of the hash. Maybe a littel more code tells it best:

ezra:~/up root# irb
irb(main):001:0> a = [1,2,3,4,5,6,7,8]
=> [1, 2, 3, 4, 5, 6, 7, 8]
irb(main):002:0> b = Hash[*a]
=> {5=>6, 1=>2, 7=>8, 3=>4}
irb(main):003:0> h1 = {"foo1"=>"bar1", "foo2"=>"bar2"}
=> {"foo1"=>"bar1", "foo2"=>"bar2"}
irb(main):004:0> h1.select { |k, v| v == "bar2" }.flatten
=> ["foo2", "bar2"]
irb(main):005:0> h2 = Hash[*h1.select { |k, v| v == "bar2" }.flatten]
=> {"foo2"=>"bar2"}
irb(main):006:0>


Cheers-
-Ezra




James Gray

1/26/2006 11:50:00 PM

0

On Jan 26, 2006, at 4:55 PM, Jon Baer wrote:

> Nice .. well that works :-) but in the constructor can you tell me
> what "*" would literally "mean"?

I see you've already got this answer.

> Does this just take only an Array obj or could you place any object
> in there w/ public attributes/methods?

I'm not sure what "this" refers to in the above sentence. Try asking
your question again, please.

> (Was this covered in pick-axe somewhere I missed?)

I just looked an can't seem to find it. It's the opposite of the *
operator described on page 347 (explodes an Array out, instead of
collecting it in).

James Edward Gray II


Pit Capitain

1/27/2006 7:21:00 AM

0

James Edward Gray II schrieb:
>> (Was this covered in pick-axe somewhere I missed?)
>
> I just looked an can't seem to find it. It's the opposite of the *
> operator described on page 347 (explodes an Array out, instead of
> collecting it in).

Pickaxe 1, page 229, "Invoking a Method". Online here:

http://phrogz.net/ProgrammingRuby/language.html#invok...

Regards,
Pit