hash key with multiple values

L

Lucky Nl

Hi ,
i have the hash result is
Code:
puts @results
[{x=>"i8"}, {y=>"i58"}, {y=>"i6"}, {y=>"v5"}, {z=>"ci5"}, {z=>"i63"},
{w=>"cie201"}]
from this @results hash ,i want to get new hash like below

Code:
puts @conver_results
[{x=>"i8"}, {y=>"i58","i6","v5"}, {z=>"ci5","i63"},  {w=>"cie201"}]

Please help me
 
J

Jean-Julien Fleck

Hello,
i have the hash result is

I would rather say that it is an array of hashes that you want to
transform into an hash of arrays.
Please help me

First, you have to get the keys that will be in the resulting hash,
for example using inject (that here merge all the hashes into one big
hashes but loosing multiple entries of the same key):

arr =3D [{x=3D>"i8"}, {y=3D>"i58"}, {y=3D>"i6"}, {y=3D>"v5"}, {z=3D>"ci5"},
{z=3D>"i63"}, {w=3D>"cie201"}]
keys =3D arr.inject {|hash,h| hash.merge(h)}.keys
p keys

Then iterate over the keys and populate the resulting hash by
selecting all the adequate values (that is those whose call don't
answer nil) and collecting them:

result_hash =3D {}
keys.each do |k|
result_hash[k] =3D arr.select {|h| h[k]}.collect {|h| h[k]}
end
p result_hash

Hope this helps.

--=20
JJ Fleck
PCSI1 Lyc=E9e Kl=E9ber
 
B

Brian Candler

Lucky said:
puts @results
[{x=>"i8"}, {y=>"i58"}, {y=>"i6"}, {y=>"v5"}, {z=>"ci5"}, {z=>"i63"},
{w=>"cie201"}]
[/code]
from this @results hash ,i want to get new hash like below

Code:
puts @conver_results
[{x=>"i8"}, {y=>"i58","i6","v5"}, {z=>"ci5","i63"},  {w=>"cie201"}]

Note that {z=>"ci5","i63"} is not valid hash syntax. But you can have
{"z"=>["ci5","i63"]}

@conver_results = Hash.new { |h,k| h[k] = [] }
@results.each { |h| h.each { |k,v| @conver_results[k] << v } }

The first line makes a Hash whose elements are automatically an empty
array if not already created. The second iterates over your original
hashes and adds to the values array for each key.

Another way:

@conver_results = {}
@results.heac { |h| h.each { |k,v|
@conver_results[k] ||= []
@conver_results[k] << v
} }
 
B

Brian Candler

BTW, the structure you have with an array of hashes, each hash with a
single k/v pair, is unusual:

@results = [{"x"=>"i8"}, {"y"=>"i58"}, {"y"=>"i6"}, {"y"=>"v5"},
{"z"=>"ci5"}, {"z"=>"i63"}, {"w"=>"cie201"}]

It would be more usual to have 2-element arrays for the k/v pairs:

@results = [["x","i8"], ["y","i58"], ["y","i6"], ["y","v5"],
["z","ci5"], ["z","i63"], ["w","cie201"]]

which in turn simplifies the processing:

@conver_results = Hash.new { |h,k| h[k] = [] }
@results.each { |k,v| @conver_results[k] << v }
 
L

Lucky Nl

Hi , thankq to all,
every solution is working

but the problem is am not getting in order means

getting result is
{"w"=>["cie201"], "x"=>["i8"], "y"=>["i58", "i6", "v5"], "z"=>["ci5",
"i63"]}

but actual order of the elements placed is
@results = [{"x"=>"i8"}, {"y"=>"i58"}, {"y"=>"i6"}, {"y"=>"v5"},
{"z"=>"ci5"}, {"z"=>"i63"}, {"w"=>"cie201"}]

so i want the order is

{ "x"=>["i8"], "y"=>["i58", "i6", "v5"], "z"=>["ci5",
"i63"],"w"=>["cie201"]}
 
R

Robert Klemme

Hi , thankq to all,
every solution is working

but the problem is am not getting in order means

getting result is
{"w"=>["cie201"], "x"=>["i8"], "y"=>["i58", "i6", "v5"], "z"=>["ci5",
"i63"]}

but actual order of the elements placed is
@results = [{"x"=>"i8"}, {"y"=>"i58"}, {"y"=>"i6"}, {"y"=>"v5"},
{"z"=>"ci5"}, {"z"=>"i63"}, {"w"=>"cie201"}]

so i want the order is

{ "x"=>["i8"], "y"=>["i58", "i6", "v5"], "z"=>["ci5",
"i63"],"w"=>["cie201"]}

In Ruby 1.9 Hash maintains insertion order so you should get what you
expect. But generally hashes are not ordered. If you want to enforce a
particular ordering then you need to explicitly sort.

Kind regards

robert
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,224
Latest member
BettieToom

Latest Threads

Top