Parsing-Newbie question

R

Ricardo Furgeson

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
 
M

Mat Schaffer

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
 
W

William James

Ricardo said:
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" ]
 
R

Ricardo Furgeson

William said:
Ricardo said:
"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?
 
D

dblack

Hi --

William said:
Ricardo said:
"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?

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

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
http://www.manning.com/black => RUBY FOR RAILS (reviewed on
Slashdot, 7/12/2006!)
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
(e-mail address removed) => me
 
W

William James

Ricardo said:
William said:
Ricardo said:
"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?

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top