Helpful Information
 
 
Category: Ruby Programming
Mix arrays into hash

Hello,

I'm trying to take data from csv and merge it into a hash so that I can make a mail merger script. The script will eventually use the key/value pairs to fill in emails from a template.

so I have email, name, id as the headings and then data in those column fields.

getting the heading is easy enough in ruby with CSV. The following will return the top row into the header variable and remove/pop it from the rest of the csv file using .shift


require 'csv'
src_data = CSV.open("somecsv","r")
header = src_data.shift

=> ["email","name","id"]


header has .each_index to then build the values hash but it doesn't work as I can't figure out a good way to use the csv row data. Here's how my mind is shaping the hash so far:


# this obviously won't work, just for mindspace
values = {}
header.each_index do |index|
values[ header[index] ] = data[index]
end

# header[0] returns email. data[0] then is the first column.


Data array isn't made here, but each time I try to loop it into the above I only get the last header variable, an irb error telling me I can't use split commands etc. I'm not seeing it. :confused:

Forget CSV for the moment and I'll try File.open etc. At any rate I'm looking for an easier way to understand how csv's can be mashed into hash either as a file object or csv object. Multiple assignment?

I'm trying the docs but I'm clearly misunderstanding the dual nature of both a heading and a column data to merge into a hash table.

Any thoughts or direction would help a lot.

cheers
sf2k

I'm not exactly sure what you are trying to accomplish with this line:
values[ header[index] ] = data[index]You would only assign three values to the hash (one for each element of header).
You may want to just iterate over the CSV object like this:
csv = CSV.open("file.csv","r")
hash = {}
csv.each do |row|
hash[row[0]] = [row[1],row[2]]
endThat gives you a hash with the keys being the email address and the values being 2-element arrays containing the name and id.
Again, I wasnt entirely clear about what you wanted so this may not fit your requirements.

Thanks for the code example! I'll play around with it and reply with my mishaps..er results. The multiple assignment aspect of it may be the key that I need.

Regarding your question about the line:


values[ header[index] ] = data[index]


It had come about from playing around in irb where I had made the hash I wanted, but where I forgot how I did it. Grrrr :rolleyes:

I have three rows in the csv indexed by 0,1,2. For each iteration of reading the row,

header[0] now becomes 'email',
header[1] becomes 'name'
header[2] becomes 'id'

data[0] is the first element of the data array giving me the actual email address.

making values a hash of "email' => "someemail@somewhere.com" and so on, for each row.

This is possible because header.each_index loops through the index numbers 0 1 2 starting it off.


Again the answer is probably some simple multiple assignment, but I haven't the understanding yet.

For anyone's reference, I'm going through the Ruby template article by Herrington from freshmeat here: http://freshmeat.net/articles/view/447/ to make a mail merger script. Combined with win32ole module, it's possible to automate Outlook... or so I believe ;)

cheers
sf2k

close.

your code creates the actual email as the key rather than the value,

so it makes some@email.com => ['derek', '1111'] rather than just email => some@email.com .

the line header.shift takes the first line out of the csv and into the header array, so I'm combining two arrays into a hash.

your code gives me ideas though, so I'll reply again when I have looked at it more closely.

Best Regards,
sf2k

I see where I misunderstood.
Here is something more along what I think you are after.


hash = {}
hash['email'] = []
hash['name'] = []
hash['id'] = []
csv.each do |row|
hash['email'] << row[0]
hash['name'] << row[1]
hash['id'] << row[2]
end
Where I hardcode 'email', 'name', and 'id' you could feel free to index however you want into the header array you rip off at the start.
That just appends to the end of each array stored in the hash variable for each row in the csv file.










privacy (GDPR)