From: Mayur Shivakumar on 17 Nov 2009 19:17 Hi , i am new to ruby and can anybody tell me way i can parse a tab delimited text file and convert it into a hash. thank you. -- Posted via http://www.ruby-forum.com/.
From: Seebs on 17 Nov 2009 19:26 On 2009-11-18, Mayur Shivakumar <sk.mayur(a)gmail.com> wrote: > i am new to ruby and can anybody tell me way i can parse a tab > delimited text file and convert it into a hash. thank you. It would depend on what was delimited by the tabs. You might try: x = File.read("data.text") x.sub!(%r{.*\n}, '#') This reads the contents of the file into a string, and turns it into a hash (#). -s -- Copyright 2009, all wrongs reversed. Peter Seebach / usenet-nospam(a)seebs.net http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
From: Jeremy Woertink on 17 Nov 2009 19:26 Mayur Shivakumar wrote: > Hi , > i am new to ruby and can anybody tell me way i can parse a tab > delimited text file and convert it into a hash. thank you. You can try filename = "my_tab_delimeted_file.txt" File.readlines(filename).each do |line| line.split("\t").each do |item| puts item end end ~Jeremy -- Posted via http://www.ruby-forum.com/.
From: Mayur Shivakumar on 17 Nov 2009 19:34 Jeremy Woertink wrote: > Mayur Shivakumar wrote: >> Hi , >> i am new to ruby and can anybody tell me way i can parse a tab >> delimited text file and convert it into a hash. thank you. > > You can try > > filename = "my_tab_delimeted_file.txt" > > File.readlines(filename).each do |line| > line.split("\t").each do |item| > puts item > end > end > > > ~Jeremy Thanks ...but i want to convert to hash.and text file might look like this name score result jermy 23 pass anooj 1 fail -- Posted via http://www.ruby-forum.com/.
From: Justin Collins on 17 Nov 2009 19:43 Mayur Shivakumar wrote: > Hi , > i am new to ruby and can anybody tell me way i can parse a tab > delimited text file and convert it into a hash. thank you. > Assuming you mean each line contains a key and a value separated by a tab, you can do something like results = Hash.new File.open "your_file.txt" do |f| f.each_line do |l| key, value = l.chomp.split("\t") results[key] = value end end p results -Justin
|
Next
|
Last
Pages: 1 2 Prev: Special characters in csv header using fastercsv Next: Using /usr/sbin/sendmail in Ruby Script |