# Parsing-Newbie question

**URL:** <https://www.ruby-forum.com/t/parsing-newbie-question/99036>\
**Category:** Ruby\
**Created:** [July 19, 2006, 10:47pm UTC](https://www.ruby-forum.com/t/parsing-newbie-question/99036 "2006-07-19T22:47:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ricardo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a6a055/32.png) [@ricardo](https://www.ruby-forum.com/u/ricardo)\
**Post date:** [July 19, 2006, 10:47pm UTC](https://www.ruby-forum.com/t/parsing-newbie-question/99036/1 "2006-07-19T22:47:24Z")

</div>

Hello everybody,

Ruby is my first prgramming lenguage. I know it’s a simple question so  
I hope someone can help me out. I have a file that i need to read,  
here’s my code so far:

class Parser

File.open(“Data”) do |file|  
while line = file.gets

end  
end

end

somewhere in my file there is a table of strings like this:

“Hello” = “hi miguel”  
“wrong” = “this is not the right anser”  
“correct asnwer” = “this is the right asnwer”  
“for what” = “tell me what you need it for”

My problem is this:  
I want o search for this table, and read in such a way so that when I  
call for a value(example, hello I can get its value, which is ‘hi  
miguel’).

I know it’s a straigt forward process. I’m not sure how to search for  
the table, and wheather to use a hash or arrays. Can anyone help me  
out?

Thank you so much.

Ricardo

---

<div class="post-metadata">

**Author:** ![ricardo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a6a055/32.png) [@ricardo](https://www.ruby-forum.com/u/ricardo)\
**Post date:** [July 19, 2006, 11:20pm UTC](https://www.ruby-forum.com/t/parsing-newbie-question/99036/2 "2006-07-19T23:20:10Z")

</div>

On Jul 19, 2006, at 4:47 PM, Ricardo F. wrote:

> while line = file.gets
> 
> I know it’s a straigt forward process. I’m not sure how to search for  
> the table, and wheather to use a hash or arrays. Can anyone help me  
> out?
> 
> Thank you so much.
> 
> Ricardo

I find that each\_line is nicer to work with than .gets for these  
purposes. It would look like this:

File.open(“Data”) do |file|  
file.each\_line do |line|  
#here pull the line appart (regular expressions are a good  
choice, or maybe using the String#split function. Check ‘ri’ for that)  
#Use the pulled apart piece to create a data structure  
end  
end

I’ll leave the middle parts to you. Be warned that the “line”  
variable will still have a new line ("\n") at the end. A mistake  
which caught me when I was first learning Ruby.

Good luck!  
Mat

---

<div class="post-metadata">

**Author:** ![ricardo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a6a055/32.png) [@ricardo](https://www.ruby-forum.com/u/ricardo)\
**Post date:** [July 20, 2006, 1:33am UTC](https://www.ruby-forum.com/t/parsing-newbie-question/99036/3 "2006-07-20T01:33:24Z")

</div>

William J. wrote:

> Ricardo F. wrote:
> 
> > “Hello” = “hi miguel”  
> > the table, and wheather to use a hash or arrays. Can anyone help me  
> > out?
> 
> table = {}  
> IO.foreach(‘Data’) { |line|  
> if line =~ /^ \s\* " (._?) " \s_ = \s\* " (.\*?) "/x  
> table[$1] = $2  
> end  
> }
> 
> puts table[“wrong”]

Thanks for the help William,

I tried the code you wrote, but I got a nil as a result…I modified the  
code as follows:

table = {}  
IO.foreach(‘Localizable.strings’){ |line|  
if line =~ /^ \s\* " (._?) " \s_ = \s\* " (._?) "\s_/  
table[$1] = $2  
end  
}

am I missing something?

---

<div class="post-metadata">

**Author:** ![ricardo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a6a055/32.png) [@ricardo](https://www.ruby-forum.com/u/ricardo)\
**Post date:** [July 20, 2006, 1:49am UTC](https://www.ruby-forum.com/t/parsing-newbie-question/99036/4 "2006-07-20T01:49:10Z")

</div>

Hi –

On Thu, 20 Jul 2006, Ricardo F. wrote:

> > ```
> > table[$1] = $2
> > 
> > ```
> 
> table = {}  
> IO.foreach(‘Localizable.strings’){ |line|  
> if line =~ /^ \s\* " (._?) " \s_ = \s\* " (._?) "\s_/  
> table[$1] = $2  
> end  
> }
> 
> am I missing something?

The /x modifier on the end of William’s regex means that literal  
whitespace is ignored. (I’m a big non-fan of /x – I find it hard to  
read regular expressions with a lot of insignificant whitespace mixed  
in – but it has a following 🙂 Without it, the regex engine will  
look for actual spaces around the various quotation marks.

David

---

<div class="post-metadata">

**Author:** ![ricardo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a6a055/32.png) [@ricardo](https://www.ruby-forum.com/u/ricardo)\
**Post date:** [July 20, 2006, 12:17am UTC](https://www.ruby-forum.com/t/parsing-newbie-question/99036/5 "2006-07-20T00:17:48Z")

</div>

Ricardo F. wrote:

> “Hello” = “hi miguel”  
> the table, and wheather to use a hash or arrays. Can anyone help me  
> out?

table = {}  
IO.foreach(‘Data’) { |line|  
if line =~ /^ \s\* " (._?) " \s_ = \s\* " (.\*?) "/x  
table[$1] = $2  
end  
}

puts table[“wrong”]

---

<div class="post-metadata">

**Author:** ![ricardo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a6a055/32.png) [@ricardo](https://www.ruby-forum.com/u/ricardo)\
**Post date:** [July 20, 2006, 9:42am UTC](https://www.ruby-forum.com/t/parsing-newbie-question/99036/6 "2006-07-20T09:42:59Z")

</div>

Ricardo F. wrote:

> > ```
> > table[$1] = $2
> > 
> > ```
> 
> table = {}  
> IO.foreach(‘Localizable.strings’){ |line|  
> if line =~ /^ \s\* " (._?) " \s_ = \s\* " (._?) "\s_/

Removing the x is like removing the lead from your  
mechanical pencil.

> ```
> table[$1] = $2
> 
> ```
> 
> end  
> }
> 
> am I missing something?
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

table = {}

# Assumes that the filename is “Data”.

IO.foreach(‘Data’) { |line|  
if line =~  
# Regular expressions contain extremely condensed code.  
# Therefore, anyone who has good sense knows that it is  
# usually a good idea to use the x modifier which allows one  
# to include whitespace and comments. A quote from  
# Perl’s creator:  
# Since /x extended syntax is now the default, # is  
# now always a metacharacter indicating a comment,  
# and whitespace is now always “meta”.  
/  
^ # Start of the string.  
\s\* # Optional whitespace (spaces or tabs).  
" # A double quote.  
(._?) # Capture the key in $1. The ? makes it non-greedy.  
" \s_ # Double quote followed by optional whitespace.  
= \s\* # Equal sign followed by optional whitespace.  
"  
(.\*?) # Capture value in $2.  
"  
/x # x for extended regular expression.

```
table[$1] = $2

```

end

}

puts table[“wrong”]  
p table
