Adding keys to a hash in a loop

Discussion in 'Ruby' started by Jack Bauer, Feb 1, 2010.

  1. Jack Bauer

    Jack Bauer Guest

    Hi all,

    I'm trying to essentially accomplish this behavior:

    output.each do |item|
    output_hash << {item.name => []}
    end

    So that I can end up with:

    {"first_set" => [], "second_set" => [], "third_set" => [], ... }

    I know the first block of code won't work, but it's just to illustrate
    what it is I wish to accomplish. Can anyone help me out? Thanks.
    --
    Posted via http://www.ruby-forum.com/.
    Jack Bauer, Feb 1, 2010
    #1
    1. Advertising

  2. On 01.02.2010 18:35, Jack Bauer wrote:

    > I'm trying to essentially accomplish this behavior:
    >
    > output.each do |item|
    > output_hash<< {item.name => []}
    > end
    >
    > So that I can end up with:
    >
    > {"first_set" => [], "second_set" => [], "third_set" => [], ... }
    >
    > I know the first block of code won't work, but it's just to illustrate
    > what it is I wish to accomplish. Can anyone help me out? Thanks.


    Erm, how would you put a key value pair into a Hash without a loop?

    http://www.ruby-doc.org/core/classes/Hash.html

    Kind regards

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Feb 1, 2010
    #2
    1. Advertising

  3. Jack Bauer

    Jack Bauer Guest

    Robert Klemme wrote:

    > Erm, how would you put a key value pair into a Hash without a loop?


    This is the loop...

    output.each do |item|
    output_hash << {item.name => []}
    end

    Iterate through items in the "output" array and create a key for the
    "output_hash" hash using the value of "item.name" as the key, and an
    empty array as the value for that key.

    I know the output_hash << won't work, that's why I'm asking how I can go
    about it. I was just using that as a sort of pseudo-code to get my point
    across.
    --
    Posted via http://www.ruby-forum.com/.
    Jack Bauer, Feb 1, 2010
    #3
  4. Jack Bauer wrote:
    > Robert Klemme wrote:
    >
    >> Erm, how would you put a key value pair into a Hash without a loop?

    >
    > This is the loop...
    >
    > output.each do |item|
    > output_hash << {item.name => []}
    > end
    >
    > Iterate through items in the "output" array and create a key for the
    > "output_hash" hash using the value of "item.name" as the key, and an
    > empty array as the value for that key.
    >
    > I know the output_hash << won't work, that's why I'm asking how I can go
    > about it. I was just using that as a sort of pseudo-code to get my point
    > across.


    The same way you assign to a hash element outside a loop:
    output_hash[item.name] = value . << is only useful with arrays, not
    hashes.

    Best,
    --
    Marnen Laibow-Koser
    http://www.marnen.org

    --
    Posted via http://www.ruby-forum.com/.
    Marnen Laibow-Koser, Feb 1, 2010
    #4
  5. Jack Bauer

    Jack Bauer Guest

    Marnen Laibow-Koser wrote:
    > The same way you assign to a hash element outside a loop:
    > output_hash[item.name] = value . << is only useful with arrays, not
    > hashes.



    Thanks, I know << is only for arrays. Using = will only end up with one
    key and not all 10 that I'm trying to get.
    --
    Posted via http://www.ruby-forum.com/.
    Jack Bauer, Feb 1, 2010
    #5
  6. Jack Bauer wrote:
    > Marnen Laibow-Koser wrote:
    >> The same way you assign to a hash element outside a loop:
    >> output_hash[item.name] = value . << is only useful with arrays, not
    >> hashes.

    >
    >
    > Thanks, I know << is only for arrays. Using = will only end up with one
    > key and not all 10 that I'm trying to get.


    Wrong. Do it inside the loop, for each key in turn. Try it!

    Best,
    --
    Marnen Laibow-Koser
    http://www.marnen.org

    --
    Posted via http://www.ruby-forum.com/.
    Marnen Laibow-Koser, Feb 1, 2010
    #6
  7. On 2010/02/01, at 13:07, Jack Bauer wrote:

    > Marnen Laibow-Koser wrote:
    >> The same way you assign to a hash element outside a loop:
    >> output_hash[item.name] = value . << is only useful with arrays, not
    >> hashes.

    >
    >
    > Thanks, I know << is only for arrays. Using = will only end up with one
    > key and not all 10 that I'm trying to get.


    Hi Jack.

    The suggestion is not to just replace << with = like this:

    output.each do |item|
    output_hash = {item.name => []}
    end

    The suggestion is to use = in a normal hash assignment:

    output.each do |item|
    output_hash[item.name] = []
    end

    HTH,
    Matt
    Matthew Pounsett, Feb 1, 2010
    #7
  8. Jack Bauer

    Jack Bauer Guest

    Matthew Pounsett wrote:
    > The suggestion is to use = in a normal hash assignment:



    There we go! Thanks!
    --
    Posted via http://www.ruby-forum.com/.
    Jack Bauer, Feb 1, 2010
    #8
  9. Jack Bauer wrote:
    > Hi all,
    >
    > I'm trying to essentially accomplish this behavior:
    >
    > output.each do |item|
    > output_hash << {item.name => []}
    > end


    Apart from the other suggestions, you can do:

    output.each do |item|
    output_hash.merge!(item.name => [])
    end
    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, Feb 1, 2010
    #9
  10. Brian Candler wrote:
    > Jack Bauer wrote:
    >> Hi all,
    >>
    >> I'm trying to essentially accomplish this behavior:
    >>
    >> output.each do |item|
    >> output_hash << {item.name => []}
    >> end

    >
    > Apart from the other suggestions, you can do:
    >
    > output.each do |item|
    > output_hash.merge!(item.name => [])
    > end


    Yes. Although merging a single-key hash seems silly, and creates extra
    objects...

    Best,
    -- 
    Marnen Laibow-Koser
    http://www.marnen.org


    --
    Posted via http://www.ruby-forum.com/.
    Marnen Laibow-Koser, Feb 1, 2010
    #10
  11. Jack Bauer

    yermej Guest

    On Feb 1, 11:35 am, Jack Bauer <> wrote:
    > Hi all,
    >
    > I'm trying to essentially accomplish this behavior:
    >
    > output.each do |item|
    >   output_hash << {item.name => []}
    > end


    Depending on how you'll be using output_hash, you might use a default
    value instead of individually initializing the values. Based on your
    sample, you can likely use:

    output_hash = Hash.new {|hash, key| hash[key] = Array.new}

    I'm guessing this is what Robert was hinting at.
    yermej, Feb 2, 2010
    #11
  12. 2010/2/2 yermej <>:
    > On Feb 1, 11:35=A0am, Jack Bauer <> wrote:
    >> Hi all,
    >>
    >> I'm trying to essentially accomplish this behavior:
    >>
    >> output.each do |item|
    >> =A0 output_hash << {item.name =3D> []}
    >> end

    >
    > Depending on how you'll be using output_hash, you might use a default
    > value instead of individually initializing the values. Based on your
    > sample, you can likely use:
    >
    > output_hash =3D Hash.new {|hash, key| hash[key] =3D Array.new}
    >
    > I'm guessing this is what Robert was hinting at.


    Actually, no. :) But you are right, this is certainly an option in
    many cases. It all depends on the situation and we do know nothing
    about the "surrounding" code.

    I rather tried to help Jack solve this for himself since it's not too
    tricky. Providing the full solution is not always the best choice
    IMHO.

    Kind regards

    robert

    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Feb 2, 2010
    #12
    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:
    517
    red floyd
    Nov 10, 2011
  2. Xeno Campanoli
    Replies:
    16
    Views:
    264
    Martin DeMello
    Aug 25, 2005
  3. Alex Fenton

    Hash#values and Hash#keys order

    Alex Fenton, Apr 7, 2006, in forum: Ruby
    Replies:
    1
    Views:
    136
    George Ogata
    Apr 15, 2006
  4. Mage

    hash.keys and hash.values

    Mage, Aug 13, 2006, in forum: Ruby
    Replies:
    14
    Views:
    175
  5. Isaac Won
    Replies:
    9
    Views:
    371
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page