[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Merge an array of hashes

cleaner416

9/12/2007 6:55:00 PM

For some reason I can't get my head wrapped around this trivial
exercise. I have an array of hashes like so

a = [ { :some_key => :some_value }, { :another_key
=> :another_value } ]

And I would like to flatten it to single hash in one line so I get

{ :some_key => :some_value, :another_key => :another_value }

Any suggestions?

9 Answers

James Gray

9/12/2007 7:00:00 PM

0

On Sep 12, 2007, at 1:55 PM, cleaner416 wrote:

> For some reason I can't get my head wrapped around this trivial
> exercise. I have an array of hashes like so
>
> a = [ { :some_key => :some_value }, { :another_key
> => :another_value } ]
>
> And I would like to flatten it to single hash in one line so I get
>
> { :some_key => :some_value, :another_key => :another_value }
>
> Any suggestions?

Sure:

>> a = [{:some_key => :some_value}, {:another_key => :another_value}]
=> [{:some_key=>:some_value}, {:another_key=>:another_value}]
>> a.inject { |all, h| all.merge(h) }
=> {:some_key=>:some_value, :another_key=>:another_value}

James Edward Gray II

Bob Showalter

9/12/2007 7:02:00 PM

0

On 9/12/07, cleaner416 <cleaner416@gmail.com> wrote:
> For some reason I can't get my head wrapped around this trivial
> exercise. I have an array of hashes like so
>
> a = [ { :some_key => :some_value }, { :another_key
> => :another_value } ]
>
> And I would like to flatten it to single hash in one line so I get
>
> { :some_key => :some_value, :another_key => :another_value }

Hash[*a.collect {|h| h.to_a}.flatten]

Luis Parravicini

9/12/2007 7:04:00 PM

0

On 9/12/07, cleaner416 <cleaner416@gmail.com> wrote:
> For some reason I can't get my head wrapped around this trivial
> exercise. I have an array of hashes like so
>
> a = [ { :some_key => :some_value }, { :another_key
> => :another_value } ]
>
> And I would like to flatten it to single hash in one line so I get
>
> { :some_key => :some_value, :another_key => :another_value }
>
> Any suggestions?

hash = Hash.new
a.each { |h| hash.merge! h }


--
Luis Parravicini
http://ktulu.co...

cleaner416

9/12/2007 7:05:00 PM

0

Thanks!!!!!!!!

Joel VanderWerf

9/12/2007 7:20:00 PM

0

Luis Parravicini wrote:
> On 9/12/07, cleaner416 <cleaner416@gmail.com> wrote:
>> For some reason I can't get my head wrapped around this trivial
>> exercise. I have an array of hashes like so
>>
>> a = [ { :some_key => :some_value }, { :another_key
>> => :another_value } ]
>>
>> And I would like to flatten it to single hash in one line so I get
>>
>> { :some_key => :some_value, :another_key => :another_value }
>>
>> Any suggestions?
>
> hash = Hash.new
> a.each { |h| hash.merge! h }

Or the equivalent using #inject, if you like:

a = [ { :some_key => :some_value }, { :another_key => :another_value } ]

merged = a.inject({}) {|acc, h| acc.merge! h}

p merged # ==> {:some_key=>:some_value, :another_key=>:another_value}

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Phrogz

9/12/2007 9:10:00 PM

0

On Sep 12, 1:20 pm, Joel VanderWerf <vj...@path.berkeley.edu> wrote:
> Luis Parravicini wrote:
> > On 9/12/07, cleaner416 <cleaner...@gmail.com> wrote:
> >> For some reason I can't get my head wrapped around this trivial
> >> exercise. I have an array of hashes like so
>
> >> a = [ { :some_key => :some_value }, { :another_key
> >> => :another_value } ]
>
> >> And I would like to flatten it to single hash in one line so I get
>
> >> { :some_key => :some_value, :another_key => :another_value }
>
> >> Any suggestions?
>
> > hash = Hash.new
> > a.each { |h| hash.merge! h }
>
> Or the equivalent using #inject, if you like:
>
> a = [ { :some_key => :some_value }, { :another_key => :another_value } ]
>
> merged = a.inject({}) {|acc, h| acc.merge! h}
>
> p merged # ==> {:some_key=>:some_value, :another_key=>:another_value}

....which brings us back (almost) to JEGII's original response :)

On Sep 12, 12:59 pm, James Edward Gray II <ja...@grayproductions.net>
wrote:
> Sure:
>
> >> a = [{:some_key => :some_value}, {:another_key => :another_value}]
> => [{:some_key=>:some_value}, {:another_key=>:another_value}]
> >> a.inject { |all, h| all.merge(h) }
> => {:some_key=>:some_value, :another_key=>:another_value}

Joel VanderWerf

9/12/2007 9:38:00 PM

0

Phrogz wrote:
> On Sep 12, 1:20 pm, Joel VanderWerf <vj...@path.berkeley.edu> wrote:
...
>> Or the equivalent using #inject, if you like:
...
> ...which brings us back (almost) to JEGII's original response :)

Sorry, my bad :/

But note that #merge! is probably slightly faster in this case than
#merge, since it reuses the same hash. This is safe in this case because
the accumulated hash is not referenced elsewhere during the iteration.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Rubén Medellín

9/13/2007 6:08:00 AM

0


>
> Hash[*a.collect {|h| h.to_a}.flatten]

I wouldn't recommend this.

a = [{:an_array => [1,2,3,4]}, {:another_array => [5,6,7,8]}]
Hash[*a.collect {|h| h.to_a}.flatten]

=> { :an_array => 1, 2 => 3, 4 => :another_array, 5 => 6, 7 => 8 }

There is no problem of course if there are no lists in the hash.

Yossef Mendelssohn

9/13/2007 2:51:00 PM

0

On Sep 13, 1:10 am, Rubén Medellín <CHub...@gmail.com> wrote:
> > Hash[*a.collect {|h| h.to_a}.flatten]
>
> I wouldn't recommend this.
>
> a = [{:an_array => [1,2,3,4]}, {:another_array => [5,6,7,8]}]
> Hash[*a.collect {|h| h.to_a}.flatten]
>
> => { :an_array => 1, 2 => 3, 4 => :another_array, 5 => 6, 7 => 8 }
>
> There is no problem of course if there are no lists in the hash.

2 is 3, 5 is 6, and 7 is 8?

My entire worldview has gone topsy-turvy.

--
-yossef