newbie qustion

R

Robert Smith

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"

I'm trying to create a hash table but i'm not sure what's wrong with my
delimeters:

class Parser

table = { }
IO.foreach('resurcefiletest') { |line|
if line =~ /^ID* (.*?) = \s* " (.*?) "/x
#if line =~ /^ \s* " (.*?) " \s* = \s* " (.*?) "/x
table[ $1 ] = $2
end
}

#puts table["StaplingTitle"]
p table


end

can somebody give me a hand?

thanks,
mike
 
E

Eric Hodel

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"

I'm trying to create a hash table but i'm not sure what's wrong
with my
delimeters:

class Parser

table = { }
IO.foreach('resurcefiletest') { |line|
if line =~ /^ID* (.*?) = \s* " (.*?) "/x
#if line =~ /^ \s* " (.*?) " \s* = \s* " (.*?) "/x
table[ $1 ] = $2
end
}

#puts table["StaplingTitle"]
p table
end

//x just gets confusing. Make your regexp simple:

table = {}

File.foreach filename do |line|
next unless line =~ /^ID(\S+) "(.*?)"/
table[$1] = $2
end
 
R

Robert Smith

Eric said:
ID_ONETHINGTOO "give me a number"
if line =~ /^ID* (.*?) = \s* " (.*?) "/x
#if line =~ /^ \s* " (.*?) " \s* = \s* " (.*?) "/x
table[ $1 ] = $2
end
}

#puts table["StaplingTitle"]
p table
end

//x just gets confusing. Make your regexp simple:

table = {}

File.foreach filename do |line|
next unless line =~ /^ID(\S+) "(.*?)"/
table[$1] = $2
end

Thanks for your help eric,

I tried what you said but nothing gets stored in the table...and i'm
nothing seeing why. do you see it?

thanks
 
W

William James

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

I'm trying to create a hash table but i'm not sure what's wrong with my
delimeters:

class Parser

table = { }
IO.foreach('resurcefiletest') { |line|
if line =~ /^ID* (.*?) = \s* " (.*?) "/x
#if line =~ /^ \s* " (.*?) " \s* = \s* " (.*?) "/x
table[ $1 ] = $2
end
}

#puts table["StaplingTitle"]
p table


end

can somebody give me a hand?

---- code: ----
table = {}

DATA.each { |line|
if line =~ /^ID ( \S+ ) \s+ " (.*) "/x
table[ $1 ] = $2
end
}
puts table.to_a.map{|a| a.join " => " }

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

---- output: ----
_ONE_THING => hello how are you
_ONETHINGTOO => give me a number
Y_SMOTHIENG_SOMETHIGN_SOMTHIGN => HELO world
S_SOMETHIGN_SOMETHING => giving you cookies in a sec
_ONEMORE_THING_ANDMORE => this are strings
X_SOMETHING_SOMETHIGN => hi again
 
R

Ryan Davis

I tried what you said but nothing gets stored in the table...and i'm
nothing seeing why. do you see it?

change the space to \s+

your original regexp didn't work because there is no =, but I agree
with eric that your use of /x makes it too complex.
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top