Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

csv to hash - ruby

Posted on 2009-05-07
5
Medium Priority
?
1,159 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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses
Course of the Month7 days, 11 hours left to enroll

824 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