Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesRuby Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today!
  #1  
Old July 12th, 2007, 03:50 AM
sf2k's Avatar
sf2k sf2k is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 173 sf2k User rank is Corporal (100 - 500 Reputation Level)sf2k User rank is Corporal (100 - 500 Reputation Level)sf2k User rank is Corporal (100 - 500 Reputation Level)sf2k User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 18 h 19 m 21 sec
Reputation Power: 5
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:
Original - 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:
Original - 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

Reply With Quote
  #2  
Old July 12th, 2007, 07:05 AM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 577 L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 8 h 49 m 8 sec
Reputation Power: 67
Send a message via AIM to L7Sqr
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.
__________________
-- I'll provide you with reference points; if they dont work, refer to something else.

If you process text, this might make your life a little easier.

Reply With Quote
  #3  
Old July 12th, 2007, 12:47 PM
sf2k's Avatar
sf2k sf2k is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 173 sf2k User rank is Corporal (100 - 500 Reputation Level)sf2k User rank is Corporal (100 - 500 Reputation Level)sf2k User rank is Corporal (100 - 500 Reputation Level)sf2k User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 18 h 19 m 21 sec
Reputation Power: 5
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:
Original - 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

Reply With Quote
  #4  
Old July 12th, 2007, 01:09 PM
sf2k's Avatar
sf2k sf2k is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 173 sf2k User rank is Corporal (100 - 500 Reputation Level)sf2k User rank is Corporal (100 - 500 Reputation Level)sf2k User rank is Corporal (100 - 500 Reputation Level)sf2k User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 18 h 19 m 21 sec
Reputation Power: 5
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

Reply With Quote
  #5  
Old July 12th, 2007, 02:08 PM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 577 L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 8 h 49 m 8 sec
Reputation Power: 67
Send a message via AIM to L7Sqr
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

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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > Mix arrays into hash


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway