Help with parsing

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/.

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/\.

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

···

On 2009-11-18, Mayur Shivakumar <sk.mayur@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.

--
Copyright 2009, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
| Seebs.Net <-- lawsuits, religion, and funny pictures
Fair game (Scientology) - Wikipedia <-- get educated!

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

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/\.

That's nice. What do you want the hash to look like?

What you show here is not the sort of thing a hash is usually much good at,
because a hash is key/value *pairs*, and you appear to have triplets.

-s

···

On 2009-11-18, Mayur Shivakumar <sk.mayur@gmail.com> wrote:

Thanks ...but i want to convert to hash.and text file might look like
this

name score result
jermy 23 pass
anooj 1 fail

--
Copyright 2009, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
| Seebs.Net <-- lawsuits, religion, and funny pictures
Fair game (Scientology) - Wikipedia <-- get educated!