Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

csv to hash - ruby

Posted on 2009-05-07
5
Medium Priority
?
1,144 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Article by: narshlob
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
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

704 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