Solved

csv to hash - ruby

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
301 redirect rails app 10 600
Which framework to go with: Ruby/Rails or Python/Django 6 721
TextArea Parameter 1 213
ruby on rails date formatting 1 152
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now