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