I’m trying to create a hash with multiple values per key from a tab
delimited file. numbers_and_colors.txt example:
1 Red
2 Blue
3 Red
2 Green
What I need is a hash that has key 2 assigned to values Blue and Green.
Running what I have below just erases the previous key, so puts
name_hash[“2”] would only show Green. Ruby example:
I need this in a hash, so is there another way to go about this? Thanks
in advance to any help, as you can probable tell I only started learning
Ruby recently.
I need this in a hash, so is there another way to go about this? Thanks
in advance to any help, as you can probable tell I only started learning
Ruby recently.
The idiom I use is to add an array to hold the values, like this:
I’m trying to create a hash with multiple values per key from a tab
delimited file. numbers_and_colors.txt example:
The idiom I use is to add an array to hold the values, like this:
(name_hash[key] ||= []) << value
hi adam -
i do what clifford does, but since i’m thick-headed i do it a bit more
verbosely (is that a word?)…
foo = Hash.new{|key, value| key[value] = []}
this sets up a hash, where each key’s value is an array (you can also
get all crazy, and make each value a hash…) once you’ve got that set,
play around with this kind of stuff…
I’m worried with the actual data this will be 100000 “Blue” and a
“Green”. Thanks for the help.
You shouldn’t print the data until you finish processing the file.
You don’t need to use “#{some-string}” to make a string into a string,
and I’ve no idea what you’re attempting by using %W[…].
You didn’t seem to understand or apply the pattern I suggested.
Basically it says: Find this array in the hash (if it doesn’t exist
then initialize it with an empty array) and append this word to the
array.
Try the following program (which contains the data file), understand
it, and if there’s something you don’t follow, come back and ask again.
The output it gives is this:
{1=>[“Red”], 2=>[“Blue”, “Green”], 3=>[“Red”]}
If you want to arrange for the entries in each array to be unique,
you’ll need to add that somehow, this doesn’t do it:
name_hash = {}
DATA.each do |file_line|
line_parts = file_line.split(/\W+/)
(name_hash[line_parts[0].to_i] ||= []) << line_parts[1]
end
name_hash = Hash.new{|key, value| key[value] = []}
File.open(“numbers_and_colors.txt”).each do |file_line|
file_line.chomp!
line_parts = file_line.split(/\t/)
name_hash["#{line_parts[0]}"] = %W["#{line_parts[1]}"]
puts name_hash[“2”]
end
I end up with
“Blue”
“Blue”
“Green”
I’m worried with the actual data this will be 100000 “Blue” and a
“Green”. Thanks for the help. And according to Merriam-Webster verbosely
is indeed a word
think i finally figured out what you were trying to do - sorry for not
reading your first post a little more carefully… try this:
hash = Hash.new{|key, value| key[value] = []}
file = File.open(“hashtext.txt”, ‘r’)
file.each do |line|
line.chomp!
parts = line.split(/\t/)
hash ["#{parts[0]}"] << “#{parts[1]}”
end
file.close
p hash[“1”]
p hash [“2”]
p hash [“3”]
=> [“red”]
[“blue”, “green”]
[“red”]
ah, and %W is just a nice way of saving on putting everything in
quotes if you’re making an array of strings…
array = %W[chunky bacon] ## notice no commas between entries
is the same as
array = [“chunky”, “bacon”]
I need this in a hash, so is there another way to go about this? Thanks
in advance to any help, as you can probable tell I only started learning
Ruby recently.
Both methods work wonderfully, thanks for the help, this was bothering
me for 2 hours. Sorry for the bother, Clifford, I tried your first
suggestion, but I had no idea about the necessary <<. Thanks again,
much appreciated!
Adam
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.