[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Parsing-Newbie question

Ricardo Furgeson

7/19/2006 8:48:00 PM

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



--
Posted via http://www.ruby-....

6 Answers

Mat Schaffer

7/19/2006 9:18:00 PM

0

On Jul 19, 2006, at 4:47 PM, Ricardo Furgeson wrote:
> 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

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

William James

7/19/2006 10:13:00 PM

0

Ricardo Furgeson wrote:
> 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?

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

puts table[ "wrong" ]

mathew

7/19/2006 10:26:00 PM

0

Ricardo Furgeson wrote:
> 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').

If you want to map a string to another string, then a Hash is your tool.
Note, however, that you'll have trouble if the same left hand string can
occur more than once.

The other part is how to parse the lines with regexps. That's trickier,
so let's go through it...

To match a quoted string, we would use "[^"]+"

The first " matches the open quote, [^"] matches any character other
than a close quote, and + means match it 1 or more times. Since we want
to grab the value, we put it in capture parentheses:

"([^"]+)"

In the middle, we match whitespace on either side of =, which is just
\s*=\s*

So:

table = Hash.new
while line = gets
m = line.match(/"([^"]+)"\s*=\s*"([^"]+)"/)
table[m[1]] = m[2] if m # only if m is not nil, i.e. we matched
end

for key in table.keys
puts "#{key} = #{table[key]}"
end

....which appears to work on your example data, outputting:

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


mathew
--
<URL:http://www.pobox.com/...
My parents went to the lost kingdom of Hyrule
and all I got was this lousy triforce.

Ricardo Furgeson

7/19/2006 11:34:00 PM

0

William James wrote:
> Ricardo Furgeson 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?

--
Posted via http://www.ruby-....

dblack

7/19/2006 11:48:00 PM

0

William James

7/20/2006 7:36:00 AM

0

Ricardo Furgeson wrote:
> William James wrote:
> > Ricardo Furgeson 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*/

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

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