#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    174
    Rep Power
    11

    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

    ruby Code:
     
    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:

    ruby Code:
     
    # 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.

    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
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Constant Limbo
    Posts
    989
    Rep Power
    363
    I'm not exactly sure what you are trying to accomplish with this line:
    Code:
    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:
    Code:
    csv = CSV.open("file.csv","r")
    hash = {}
    csv.each do |row|
       hash[row[0]] = [row[1],row[2]]
    end
    That 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.
    True happiness is not getting what you want, it's wanting what you've already got.

    My Blog
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    174
    Rep Power
    11
    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:

    ruby Code:
     
    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

    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
    Last edited by sf2k; July 12th, 2007 at 05:14 PM. Reason: added link
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    174
    Rep Power
    11
    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
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Constant Limbo
    Posts
    989
    Rep Power
    363
    I see where I misunderstood.
    Here is something more along what I think you are after.

    [code=ruby]hash = {}
    hash['email'] = []
    hash['name'] = []
    hash['id'] = []
    csv.each do |row|
    hash['email'] << row[0]
    hash['name'] << row[1]
    hash['id'] << row[2]
    end[/code]
    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.
    True happiness is not getting what you want, it's wanting what you've already got.

    My Blog

IMN logo majestic logo threadwatch logo seochat tools logo