Merge an array of hashes

Discussion in 'Ruby' started by cleaner416, Sep 12, 2007.

  1. cleaner416

    cleaner416 Guest

    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?
     
    cleaner416, Sep 12, 2007
    #1
    1. Advertising

  2. 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
     
    James Edward Gray II, Sep 12, 2007
    #2
    1. Advertising

  3. 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]
     
    Bob Showalter, Sep 12, 2007
    #3
  4. 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/
     
    Luis Parravicini, Sep 12, 2007
    #4
  5. cleaner416

    cleaner416 Guest

    Thanks!!!!!!!!
     
    cleaner416, Sep 12, 2007
    #5
  6. 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
     
    Joel VanderWerf, Sep 12, 2007
    #6
  7. cleaner416

    Phrogz Guest

    On Sep 12, 1:20 pm, Joel VanderWerf <> wrote:
    > 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}


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

    On Sep 12, 12:59 pm, James Edward Gray II <>
    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}
     
    Phrogz, Sep 12, 2007
    #7
  8. Phrogz wrote:
    > On Sep 12, 1:20 pm, Joel VanderWerf <> 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
     
    Joel VanderWerf, Sep 12, 2007
    #8

  9. >
    > 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.
     
    Rubén Medellín, Sep 13, 2007
    #9
  10. On Sep 13, 1:10 am, Rub=E9n Medell=EDn <> 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
     
    Yossef Mendelssohn, Sep 13, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Holness

    Hashes of Hashes via subs

    Ben Holness, Oct 5, 2003, in forum: Perl
    Replies:
    8
    Views:
    578
    Ben Holness
    Oct 8, 2003
  2. kazaam
    Replies:
    12
    Views:
    281
    Matthias Wächter
    Sep 13, 2007
  3. Matt Brooks
    Replies:
    16
    Views:
    237
    Matt Brooks
    Sep 16, 2009
  4. Tim O'Donovan

    Hash of hashes, of hashes, of arrays of hashes

    Tim O'Donovan, Oct 27, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    221
  5. PerlFAQ Server

    FAQ 4.56 How do I merge two hashes?

    PerlFAQ Server, Mar 9, 2011, in forum: Perl Misc
    Replies:
    2
    Views:
    158
    C.DeRykus
    Mar 9, 2011
Loading...

Share This Page