Hash#each with nested array as value

Discussion in 'Ruby' started by Earle Clubb, Oct 17, 2007.

  1. Earle Clubb

    Earle Clubb Guest

    I have a hash where the values are nested arrays. I'd like to be able to
    iterate over the hash and have 3 vars available in the block: key,
    value[0], and value[1]. As you can see, h.each {|a,b,c|...} doesn't
    work. Is there a better way to do it than the last line below? Thanks.

    Earle

    --------------------

    irb(main):001:0> require 'pp'
    => true
    irb(main):002:0> h = {0 => [[1], [2]]}
    => {0=>[[1], [2]]}
    irb(main):003:0> h.each {|a, b| pp a, b}
    0
    [[1], [2]]
    => {0=>[[1], [2]]}
    irb(main):004:0> h.each {|a, b, c| pp a, b, c}
    0
    [[1], [2]]
    nil
    => {0=>[[1], [2]]}
    irb(main):005:0> h.each {|a, b| pp a, b[0], b[1]}
    0
    [1]
    [2]
    => {0=>[[1], [2]]}

    --------------------
    --
    Posted via http://www.ruby-forum.com/.
     
    Earle Clubb, Oct 17, 2007
    #1
    1. Advertising

  2. Earle Clubb

    Phrogz Guest

    On Oct 17, 9:45 am, Earle Clubb <> wrote:
    > I have a hash where the values are nested arrays. I'd like to be able to
    > iterate over the hash and have 3 vars available in the block: key,
    > value[0], and value[1]. As you can see, h.each {|a,b,c|...} doesn't
    > work.


    Not much better, but:

    irb(main):006:0> h = { :a => [:foo, :bar] }
    => {:a=>[:foo, :bar]}

    irb(main):007:0> h.each{ |key,pair| a,b = *pair; p key, a, b }
    :a
    :foo
    :bar
     
    Phrogz, Oct 17, 2007
    #2
    1. Advertising

  3. On 10/17/07, Earle Clubb <> wrote:
    > I have a hash where the values are nested arrays. I'd like to be able to
    > iterate over the hash and have 3 vars available in the block: key,
    > value[0], and value[1]. As you can see, h.each {|a,b,c|...} doesn't
    > work. Is there a better way to do it than the last line below? Thanks.
    >
    > Earle


    Use parentheses.

    >> h = [0 => [[1],[2]]}
    >> h.each{|k,(a,b)| p k, a, b}

    0
    [1]
    [2]
    => {0=>[[1], [2]]}

    Regards,

    Gordon
    >>
     
    Gordon Thiesfeld, Oct 17, 2007
    #3
  4. Earle Clubb

    Earle Clubb Guest

    Gordon Thiesfeld wrote:
    > On 10/17/07, Earle Clubb <> wrote:
    >> I have a hash where the values are nested arrays. I'd like to be able to
    >> iterate over the hash and have 3 vars available in the block: key,
    >> value[0], and value[1]. As you can see, h.each {|a,b,c|...} doesn't
    >> work. Is there a better way to do it than the last line below? Thanks.
    >>
    >> Earle

    >
    > Use parentheses.
    >
    >>> h = [0 => [[1],[2]]}
    >>> h.each{|k,(a,b)| p k, a, b}

    > 0
    > [1]
    > [2]
    > => {0=>[[1], [2]]}
    >
    > Regards,
    >
    > Gordon


    Perfect. Thanks, Gordon.

    Earle
    --
    Posted via http://www.ruby-forum.com/.
     
    Earle Clubb, Oct 17, 2007
    #4
    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. rp
    Replies:
    1
    Views:
    583
    red floyd
    Nov 10, 2011
  2. Srijayanth Sridhar
    Replies:
    19
    Views:
    670
    David A. Black
    Jul 2, 2008
  3. Patrick Doyle

    Hash#each vs Hash#each_pair

    Patrick Doyle, Aug 18, 2008, in forum: Ruby
    Replies:
    11
    Views:
    248
    Michael Fellinger
    Aug 18, 2008
  4. Hunt Jon

    hash.each and hash.each_pair

    Hunt Jon, Dec 18, 2008, in forum: Ruby
    Replies:
    2
    Views:
    396
    Simon Krahnke
    Dec 20, 2008
  5. Igor Nn
    Replies:
    7
    Views:
    464
    Johnny Morrice
    May 28, 2011
Loading...

Share This Page