Solved

csv to hash - ruby

Posted on 2009-05-07
5
1,107 Views
Last Modified: 2012-05-06
Hello
How can I now retrieve certain keys and there values from the hash - tried, but keep getting an error
require 'csv' 
file = "C:/Documents and Settings/p.sivyer/RUBY/RUBY_WORKING_MODELS/csv_master.txt" 
rows = CSV::Reader.parse(File.open(file)).to_a
cols = rows.shift
collection = rows.collect do |row|  
Hash[*cols.zip(row).flatten]
end 
puts collection.first.inspect

Open in new window

0
Comment
Question by:philsivyer
  • 3
  • 2
5 Comments
 
LVL 12

Expert Comment

by:cminear
ID: 24336576
What was the error that you got?  What version of Ruby are you running?

I took your code, and just changed the filename and I got a valid response.  I tried this with ruby-1.8.6-p287 and ruby 1.8.7 p72.  Of course, those were on Solaris systems, not Windows.

I installed ruby 1.8.7 p72 on Windows, and again the program worked for me.  Separately, I also changed the 'file' variable to use single quotes and backslashes, and that worked again.  (See below.)  However, I don't think that's your problem.  The error should really point us to the problem.

(While we're at it, what Windows version are you running?  Again, I don't think that should matter, but you never know.)
require 'csv' 
file = 'C:\Documents and Settings\p.sivyer\RUBY\RUBY_WORKING_MODELS\csv_master.txt' 
rows = CSV::Reader.parse(File.open(file)).to_a
cols = rows.shift
collection = rows.collect do |row|  
Hash[*cols.zip(row).flatten]
end 
puts collection.first.inspect

Open in new window

0
 

Author Comment

by:philsivyer
ID: 24352914
Hello
I may have mislead you - yes, the script runs fine. Now, I need to know how to extract a given key or keys and to either show the keys with their values or just the values.
0
 
LVL 12

Accepted Solution

by:
cminear earned 500 total points
ID: 24354851
There are a few different options, depending on exactly what you want.  (I'll assume "Col1" and "Col3" are names of keys you want below.)

If you know the keys that you want, you obviously can just use them.  (See first example below.)

However, the best general solution is probably just using 'each' (which is synonymous with 'each_pair' with a Hash).  Note the use of two variables in the block.

(Or did I miss the point again?)
# just using the keys
puts "Col1: #{collection.first["Col1"]}\tCol3: #{collection.first["Col3"]}"
 
# using 'each' to output each and every hash value
collection.each {|row|  # each row is a hash
  row.each {|key, value|
    if key == 'col1' or key == 'Col3'
      puts "#{key}: #{value}"
    end
  }
  puts "-----"
}

Open in new window

0
 

Author Comment

by:philsivyer
ID: 24364202
Thanks for this - just the job.

0
 

Author Closing Comment

by:philsivyer
ID: 31579198
Thanks for the help
Phil
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
no images after rails 3.1 deploy 7 224
Choosing the right Ubuntu system to run Ruby-on-Rails 2 603
view ruby on rails on a browser 2 428
ruby on rails date formatting 1 163
I recently rediscovered rails when I needed a holiday project and decided to build a management dashboard for the company where I work.  With it being a project done in my free time, I could focus my time on learning the basics rather than trying to…
In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below: http://www.rubytips.org/2008/05/13/accessing-windows-api-fro…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question