Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Merge an array of hashes

Reply
Thread Tools

Merge an array of hashes

 
 
cleaner416
Guest
Posts: n/a
 
      09-12-2007
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?

 
Reply With Quote
 
 
 
 
James Edward Gray II
Guest
Posts: n/a
 
      09-12-2007
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

 
Reply With Quote
 
 
 
 
Bob Showalter
Guest
Posts: n/a
 
      09-12-2007
On 9/12/07, 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 }


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

 
Reply With Quote
 
Luis Parravicini
Guest
Posts: n/a
 
      09-12-2007
On 9/12/07, 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?


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


--
Luis Parravicini
http://ktulu.com.ar/blog/

 
Reply With Quote
 
cleaner416
Guest
Posts: n/a
 
      09-12-2007
Thanks!!!!!!!!

 
Reply With Quote
 
Joel VanderWerf
Guest
Posts: n/a
 
      09-12-2007
Luis Parravicini wrote:
> On 9/12/07, 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?

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

 
Reply With Quote
 
Phrogz
Guest
Posts: n/a
 
      09-12-2007
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}


 
Reply With Quote
 
Joel VanderWerf
Guest
Posts: n/a
 
      09-12-2007
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

 
Reply With Quote
 
Rubén Medellín
Guest
Posts: n/a
 
      09-13-2007

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

 
Reply With Quote
 
Yossef Mendelssohn
Guest
Posts: n/a
 
      09-13-2007
On Sep 13, 1:10 am, Rub=E9n Medell=EDn <CHub...@gmail.com> wrote:
> > Hash[*a.collect {|h| h.to_a}.flatten]

>
> I wouldn't recommend this.
>
> a =3D [{:an_array =3D> [1,2,3,4]}, {:another_array =3D> [5,6,7,8]}]
> Hash[*a.collect {|h| h.to_a}.flatten]
>
> =3D> { :an_array =3D> 1, 2 =3D> 3, 4 =3D> :another_array, 5 =3D> 6, 7 =3D=
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 4.56 How do I merge two hashes? PerlFAQ Server Perl Misc 2 03-09-2011 08:39 PM
Array of Hashes in an array of hashes - Complicated! Matt Brooks Ruby 16 09-16-2009 05:53 PM
How to make an array of hashes to a single array with all thevalues of these hashes ? kazaam Ruby 12 09-13-2007 01:30 PM
Hash of hashes, of hashes, of arrays of hashes Tim O'Donovan Perl Misc 5 10-28-2005 05:59 AM
Hashes of Hashes via subs Ben Holness Perl 8 10-08-2003 06:57 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57