Combining Hash elements to produce single line output

Discussion in 'Ruby' started by Paul, Apr 13, 2007.

  1. Paul

    Paul Guest

    Hi there. I know I've done something similar to this before but I
    seem to be stumped at the moment and thought I would ask for help.

    I have a temporary Hash array that holds information in different
    elements that need to be combined to produce single line outputs.
    Here's some detail:

    tmp_hash =
    [["foo:1", "123"],
    ["foo:2", "abc"],
    ["bar:1", "456"],
    ["bar:2", "xyz"]]

    Desired Output:
    ---
    "foo" "123" "abc"
    "bar" "456" "xyz"
    ---

    Sample code:
    ---
    content_alpha = ''
    content_num = ''
    tmp_hash.sort.each do |key, value|
    (name, num) = key.split(':')
    #
    content_alpha = value if ( num == '1' )
    content_num = value if ( num == '2' )
    #
    print name + "\t" + content_num + "\t" + content_alpha
    end
    ---

    I know the above doesn't produce the desired output but I haven't been
    able to crack it yet. (I've tried several different solutions but am
    still new to Ruby and scripting in general.) I was hoping someone
    might be able to provide me with some new suggestions to try.
     
    Paul, Apr 13, 2007
    #1
    1. Advertising

  2. Paul

    Peter Szinek Guest

    Paul wrote:
    > Hi there. I know I've done something similar to this before but I
    > seem to be stumped at the moment and thought I would ask for help.
    >
    > I have a temporary Hash array that holds information in different
    > elements that need to be combined to produce single line outputs.
    > Here's some detail:
    >
    > tmp_hash =
    > [["foo:1", "123"],
    > ["foo:2", "abc"],
    > ["bar:1", "456"],
    > ["bar:2", "xyz"]]
    >

    Whew, this will be ugly... I am sure somebody has a more effective
    solution, but until then:

    to_hash = tmp_hash.inject(Hash.new('')) {|hash, arr|
    key = arr[0].scan(/(.+):/)[0][0]
    hash[key] = hash[key]+(val = hash[key] == '' ? '' : ',')+arr[1]
    hash }



    Cheers,
    Peter
    __
    http://www.rubyrailways.com :: Ruby and Web2.0 blog
    http://scrubyt.org :: Ruby web scraping framework
    http://rubykitchensink.ca/ :: The indexed archive of all things Ruby
     
    Peter Szinek, Apr 13, 2007
    #2
    1. Advertising

  3. n Apr 13, 2007, at 1:30 PM, Paul wrote:

    > Hi there. I know I've done something similar to this before but I
    > seem to be stumped at the moment and thought I would ask for help.
    >
    > I have a temporary Hash array that holds information in different
    > elements that need to be combined to produce single line outputs.
    > Here's some detail:
    >
    > tmp_hash =
    > [["foo:1", "123"],
    > ["foo:2", "abc"],
    > ["bar:1", "456"],
    > ["bar:2", "xyz"]]
    >
    > Desired Output:
    > ---
    > "foo" "123" "abc"
    > "bar" "456" "xyz"
    > ---


    >> tmp_hash =

    ?> [["foo:1", "123"],
    ?> ["foo:2", "abc"],
    ?> ["bar:1", "456"],
    ?> ["bar:2", "xyz"]]
    => [["foo:1", "123"], ["foo:2", "abc"], ["bar:1", "456"], ["bar:2",
    "xyz"]]
    >> require "enumerator"

    => true
    >> tmp_hash.each_slice(2) do |alpha, num|

    ?> puts [ alpha.first[/^\w+/],
    ?> alpha.last,
    ?> num.last ].map { |f| %Q{"#{f}"} }.join("\t")
    >> end

    "foo" "123" "abc"
    "bar" "456" "xyz"
    => nil

    Hope that helps.

    James Edward Gray II
     
    James Edward Gray II, Apr 13, 2007
    #3
  4. On Sat, Apr 14, 2007 at 03:30:05AM +0900, Paul wrote:
    > tmp_hash =
    > [["foo:1", "123"],
    > ["foo:2", "abc"],
    > ["bar:1", "456"],
    > ["bar:2", "xyz"]]
    >
    > Desired Output:
    > ---
    > "foo" "123" "abc"
    > "bar" "456" "xyz"
    > ---


    # This is a hash which auto-creates non-existing members as an empty array
    new_hash = Hash.new { |h,k| h[k] = [] }

    tmp_hash.each do |key, value|
    key, rest = key.split(':', 2)
    new_hash[key] << value
    end

    new_hash.each do |key, values|
    puts '"' + ([key] + values).join('" "') + '"'
    end
     
    Brian Candler, Apr 13, 2007
    #4
  5. On 13.04.2007 20:26, Paul wrote:
    > Hi there. I know I've done something similar to this before but I
    > seem to be stumped at the moment and thought I would ask for help.
    >
    > I have a temporary Hash array that holds information in different
    > elements that need to be combined to produce single line outputs.
    > Here's some detail:
    >
    > tmp_hash =
    > [["foo:1", "123"],
    > ["foo:2", "abc"],
    > ["bar:1", "456"],
    > ["bar:2", "xyz"]]


    This is not a Hash.

    I suggest you choose a more appropriate data structure, use each key
    only once and stuff all values in an Array, i.e.

    tmp = {
    "foo" => %w{123 abc},
    "bar" => %w{456 xyz}
    }

    or

    tmp = [
    ["foo", %w{123 abc}],
    ["bar", %w{456 xyz}]
    ]

    and then

    tmp.each do |h,k|
    print h.inspect,
    " ",
    k.map {|kk| kk.inspect}.join(" "),
    "\n"
    end

    or even

    tmp.each {|a| print '"', a.flatten.join('" "'), "\"\n"}

    > Desired Output:
    > ---
    > "foo" "123" "abc"
    > "bar" "456" "xyz"
    > ---


    Kind regards

    robert
     
    Robert Klemme, Apr 13, 2007
    #5
  6. Paul

    Peter Szinek Guest

    Peter Szinek wrote:
    > Paul wrote:
    >> Hi there. I know I've done something similar to this before but I
    >> seem to be stumped at the moment and thought I would ask for help.
    >>
    >> I have a temporary Hash array that holds information in different
    >> elements that need to be combined to produce single line outputs.
    >> Here's some detail:
    >>
    >> tmp_hash =
    >> [["foo:1", "123"],
    >> ["foo:2", "abc"],
    >> ["bar:1", "456"],
    >> ["bar:2", "xyz"]]
    >>

    > Whew, this will be ugly... I am sure somebody has a more effective
    > solution, but until then:
    >
    > to_hash = tmp_hash.inject(Hash.new('')) {|hash, arr|
    > key = arr[0].scan(/(.+):/)[0][0]
    > hash[key] = hash[key]+(val = hash[key] == '' ? '' : ',')+arr[1]
    > hash }


    Sorry this does not answer your question :) It confused me that the
    original array was named 'hash' so for some reason I have thought you
    need a hash...

    Cheers,
    Peter
    __
    http://www.rubyrailways.com :: Ruby and Web2.0 blog
    http://scrubyt.org :: Ruby web scraping framework
    http://rubykitchensink.ca/ :: The indexed archive of all things Ruby
     
    Peter Szinek, Apr 13, 2007
    #6
    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. Richard
    Replies:
    2
    Views:
    589
    informant
    Feb 10, 2004
  2. Replies:
    5
    Views:
    496
    Luc The Perverse
    Oct 27, 2006
  3. rp
    Replies:
    1
    Views:
    596
    red floyd
    Nov 10, 2011
  4. Arvin Portlock
    Replies:
    6
    Views:
    152
    Arvin Portlock
    Sep 2, 2005
  5. bernd
    Replies:
    0
    Views:
    650
    bernd
    Apr 24, 2012
Loading...

Share This Page