Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Combining Hash elements to produce single line output

Reply
Thread Tools

Combining Hash elements to produce single line output

 
 
Paul
Guest
Posts: n/a
 
      04-13-2007
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.

 
Reply With Quote
 
 
 
 
Peter Szinek
Guest
Posts: n/a
 
      04-13-2007
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



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

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      04-13-2007
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

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      04-13-2007
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
 
Reply With Quote
 
Peter Szinek
Guest
Posts: n/a
 
      04-13-2007
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

 
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
Tied hash: Differentiating between assignment of single value andentire hash bernd Perl Misc 0 04-24-2012 02:41 PM
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
can java produce .exe? if it can produce jar,how do you do? aungkopyay@gmail.com Java 5 10-27-2006 02:07 AM
Combining multiple hash references into one hash reference Arvin Portlock Perl Misc 6 09-02-2005 07:18 PM
Re: howto: combining form output from one of more fields to a single line Richard HTML 2 02-10-2004 02:18 AM



Advertisments