Justin Collins
8/2/2006 8:47:00 PM
Robert Smith wrote:
> I'm trying to parse a file that looks like this:
>
> text....
> text....
> numbers....
> blalba bla
>
> and then somewhere in the file i have a table like this:
>
> IDS_SOMETHIGN_SOMETHING "giving you cookies in a sec"
> IDX_SOMETHING_SOMETHIGN "hi again"
> IDY_SMOTHIENG_SOMETHIGN_SOMTHIGN "HELO world"
> ID_ONE_THING "hello how are you"
> ID_ONETHINGTOO "give me a number"
> ID_ONEMORE_THING_ANDMORE "this are strings"
>
> I'm trying to create a hash table but i'm not sure what's wrong with my
> delimeters:
>
> class Parser
> table = {}
>
> File.foreach filename do |line|
> next unless line =~ /^ID(\S+) "(.*?)"/
> table[$1] = $2
> end
>
> end
>
> this doesn't store anything in the table...any help?
> thanks
Hi Robert,
Isn't this the third time you've posted the same question?
The problem I see is that you now are expecting ID followed by one or
more non-whitespace characters, followed by EXACTLY one space, the the
quotes.
Change
/^ID(\S+) "(.*?)"/
to
/^ID(\S+)\s+"(.*?)"/
(I've tested this with your sample input and it worked fine, assuming
there is no space between the beginning of the line and 'ID...')
Don't forget to output the table somewhere in able to see if it's
working (your current code has no output).
Hope that helps.
-Justin