parsing with delimeters ---new programmer

R

Robert Smith

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
 
T

Thomas, Mark - BLS CTR

Robert said:
and then somewhere in the file i have a table like this:
=20
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"
=20
I'm trying to create a hash table but i'm not sure what's=20
wrong with my
delimeters:
=20
class Parser
table =3D {}
=20
File.foreach filename do |line|
next unless line =3D~ /^ID(\S+) "(.*?)"/
table[$1] =3D $2
end
=20
end

Your regular expression does not accommodate more than one space between
the key and the value, which all your lines have. Also, if there are
leading spaces, you need to accommodate them.

- Mark.
 
M

Matt Todd

Is your Regexp in multiline mode? (Sorry, I can't remember if it's the
default or not...)

M.T.
 
J

Justin Collins

Robert said:
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
 
W

William James

Robert said:
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:

Ricardo, on Aug. 1 you posted this:
I'm trying to parse a file that looks like this:

text....
text....
numbers....

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"

Why did you ignore the answers that you were given at that time?
You should not have started a new thread.
 
H

Harry

Why did you ignore the answers that you were given at that time?
You should not have started a new thread.


I wondered the same thing. But the answers don't seem to be showing up
at ruby-forum. Maybe he is not seeing them.

Harry
 
J

Justin Collins

Harry said:
I wondered the same thing. But the answers don't seem to be showing up
at ruby-forum. Maybe he is not seeing them.

Harry
Yes, I've sent an email to Andreas about it (the emails not showing up
on the forum).

-Justin
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top