# Help with parsing

**URL:** <https://www.ruby-forum.com/t/help-with-parsing/179121>\
**Category:** Ruby\
**Created:** [November 18, 2009, 1:17am UTC](https://www.ruby-forum.com/t/help-with-parsing/179121 "2009-11-18T01:17:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mayur\_sk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cdc98d/32.png) [@mayur\_sk](https://www.ruby-forum.com/u/mayur_sk)\
**Post date:** [November 18, 2009, 1:17am UTC](https://www.ruby-forum.com/t/help-with-parsing/179121/1 "2009-11-18T01:17:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mayur\_sk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cdc98d/32.png) [@mayur\_sk](https://www.ruby-forum.com/u/mayur_sk)\
**Post date:** [November 18, 2009, 1:26am UTC](https://www.ruby-forum.com/t/help-with-parsing/179121/2 "2009-11-18T01:26:47Z")

</div>

Mayur S. 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

---

<div class="post-metadata">

**Author:** ![mayur\_sk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cdc98d/32.png) [@mayur\_sk](https://www.ruby-forum.com/u/mayur_sk)\
**Post date:** [November 18, 2009, 1:34am UTC](https://www.ruby-forum.com/t/help-with-parsing/179121/3 "2009-11-18T01:34:32Z")

</div>

Jeremy W. wrote:

> Mayur S. 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

---

<div class="post-metadata">

**Author:** ![mayur\_sk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cdc98d/32.png) [@mayur\_sk](https://www.ruby-forum.com/u/mayur_sk)\
**Post date:** [November 18, 2009, 1:42am UTC](https://www.ruby-forum.com/t/help-with-parsing/179121/4 "2009-11-18T01:42:49Z")

</div>

On 2009-11-18, Mayur S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![mayur\_sk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cdc98d/32.png) [@mayur\_sk](https://www.ruby-forum.com/u/mayur_sk)\
**Post date:** [November 18, 2009, 1:44am UTC](https://www.ruby-forum.com/t/help-with-parsing/179121/5 "2009-11-18T01:44:35Z")

</div>

Mayur S. 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

---

<div class="post-metadata">

**Author:** ![mayur\_sk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cdc98d/32.png) [@mayur\_sk](https://www.ruby-forum.com/u/mayur_sk)\
**Post date:** [November 18, 2009, 2:15am UTC](https://www.ruby-forum.com/t/help-with-parsing/179121/6 "2009-11-18T02:15:36Z")

</div>

On 2009-11-18, Mayur S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

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
