Solved

csv to hash - ruby

Posted on 2009-05-07
5
1,115 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Session clean at  RAILS server restart 9 832
Ruby on Rails installation Help 3 682
view ruby on rails on a browser 2 433
Very quick question about Compass :) 1 217
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 video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

828 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