Parsing-Newbie question

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

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

···

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

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" ]

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

Hi --

···

On Thu, 20 Jul 2006, 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*/
   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 :slight_smile: Without it, the regex engine will
look for actual spaces around the various quotation marks.

David

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
Ruby for Rails => RUBY FOR RAILS (reviewed on
                                     Slashdot, 7/12/2006!)
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
dblack@wobblini.net => me

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