[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

newbie qustion

Ricardo Furgeson

8/1/2006 10:35:00 PM

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


--
Posted via http://www.ruby-....

4 Answers

Eric Hodel

8/1/2006 10:42:00 PM

0

On Aug 1, 2006, at 3:35 PM, Robert Smith wrote:

> 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

--
Eric Hodel - drbrain@segment7.net - http://blog.se...
This implementation is HODEL-HASH-9600 compliant

http://trackmap.rob...



Ricardo Furgeson

8/1/2006 11:54:00 PM

0

Eric Hodel wrote:
> On Aug 1, 2006, at 3:35 PM, Robert Smith wrote:
>
>> 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

--
Posted via http://www.ruby-....

William James

8/2/2006 6:54:00 AM

0

Robert Smith wrote:
> 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

Ryan Davis

8/4/2006 5:12:00 PM

0


On Aug 1, 2006, at 4:54 PM, Robert Smith wrote:

> Eric Hodel wrote:
>> On Aug 1, 2006, at 3:35 PM, Robert Smith wrote:
>>
>>> if line =~ /^ID* (.*?) = \s* " (.*?) "/x

>> next unless line =~ /^ID(\S+) "(.*?)"/
>
> 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.