# Subselecting hash back into hash (oneliner?)

**URL:** <https://www.ruby-forum.com/t/subselecting-hash-back-into-hash-oneliner/53154>\
**Category:** Ruby\
**Created:** [January 26, 2006, 9:01pm UTC](https://www.ruby-forum.com/t/subselecting-hash-back-into-hash-oneliner/53154 "2006-01-26T21:01:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonbaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@jonbaer](https://www.ruby-forum.com/u/jonbaer)\
**Post date:** [January 26, 2006, 9:01pm UTC](https://www.ruby-forum.com/t/subselecting-hash-back-into-hash-oneliner/53154/1 "2006-01-26T21:01:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jonbaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@jonbaer](https://www.ruby-forum.com/u/jonbaer)\
**Post date:** [January 26, 2006, 9:07pm UTC](https://www.ruby-forum.com/t/subselecting-hash-back-into-hash-oneliner/53154/2 "2006-01-26T21:07:54Z")

</div>

On Jan 26, 2006, at 1:59 PM, Jon B. 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 G. II

---

<div class="post-metadata">

**Author:** ![jonbaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@jonbaer](https://www.ruby-forum.com/u/jonbaer)\
**Post date:** [January 26, 2006, 9:20pm UTC](https://www.ruby-forum.com/t/subselecting-hash-back-into-hash-oneliner/53154/3 "2006-01-26T21:20:05Z")

</div>

Jon B. wrote:

> Thanks.
> 
> - Jon

Perhaps:

h2 = Hash.new  
h1 = {“foo1”=\>“bar1”, “foo2”=\>“bar2”}  
h2 = h1.delete\_if{|k,v| v != “bar2”}  
p h2

---

<div class="post-metadata">

**Author:** ![jonbaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@jonbaer](https://www.ruby-forum.com/u/jonbaer)\
**Post date:** [January 26, 2006, 11:57pm UTC](https://www.ruby-forum.com/t/subselecting-hash-back-into-hash-oneliner/53154/4 "2006-01-26T23:57:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jonbaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@jonbaer](https://www.ruby-forum.com/u/jonbaer)\
**Post date:** [January 27, 2006, 12:51am UTC](https://www.ruby-forum.com/t/subselecting-hash-back-into-hash-oneliner/53154/5 "2006-01-27T00:51:37Z")

</div>

On Jan 26, 2006, at 4:55 PM, Jon B. 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 G. II

---

<div class="post-metadata">

**Author:** ![jonbaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@jonbaer](https://www.ruby-forum.com/u/jonbaer)\
**Post date:** [January 27, 2006, 12:12am UTC](https://www.ruby-forum.com/t/subselecting-hash-back-into-hash-oneliner/53154/6 "2006-01-27T00:12:25Z")

</div>

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

> - Jon
> 
> On Jan 26, 2006, at 3:06 PM, James Edward G. 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

---

<div class="post-metadata">

**Author:** ![jonbaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@jonbaer](https://www.ruby-forum.com/u/jonbaer)\
**Post date:** [January 27, 2006, 8:23am UTC](https://www.ruby-forum.com/t/subselecting-hash-back-into-hash-oneliner/53154/7 "2006-01-27T08:23:52Z")

</div>

James Edward G. 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:

[The Ruby Language](http://phrogz.net/ProgrammingRuby/language.html#invokingamethod)

Regards,  
Pit
