[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Trouble parsing against a hash

Peter Bailey

8/14/2006 4:43:00 PM

Hello,
I get the following error message from RUBY when I try to run the script
shown below. Sorry, the hash is huge.

I need to match the beginning of file names with the keys in a hash. If
there's a match, then I need to pull the value for that matching key.
That's all really.

Thanks,
Peter


Error:
test2.rb:7: parse error, unexpected kEND, expecting '}'


Ruby Script:
1 require 'FileUtils'
2 Dir.chdir('u:/indexes')
3 fileshash = { :aacmc7p => 'aacm00', :acm059p => 'bacm00', :acm061p =>
'bacm00', :acmc59p => 'bacm00', :acmt59p => 'bacm00', :adc064p =>
'adam00', :adm064p => 'adam00', :adm065p => 'adam00', :apr005p =>
'tmap00', :atc071p => 'atrr00', :bac054p => 'bnkr00', :bkc031p =>
'bblr00', :bpc061p => 'btm000', :cal158p => 'clas00', :car125p =>
'care00', :cbncc7p => 'cbnc00', :cgmprp_ => 'cglw00', :chc074p =>
'chem00', :chc075p => 'chem00', :clc017p => 'clr000', :crc074p =>
'crl000', :ctl157p => 'ctlr00', :dec079p => 'der000', :dlc080p =>
'dlr000', :dtc069p => 'dt0000', :edc131p => 'eddg00', :edr094p =>
'edr000', :edt131p => 'eddg00', :eeomc7p => 'eeom00', :eip066p =>
'eplr00', :emgc15p => 'emgd00', :erc023p => 'er0000', :ercr25p =>
'er0000', :exe155p => 'exer00', :fcc093p => 'fcr000', :flc046p =>
'flr000', :gec099p => 'gerr00', :hac076p => 'hzrf00', :hcc201p =>
'hccg00', :hcci01p => 'hccg00', :hcp030p => 'hce000', :hct201p =>
'hccg00', :hcti01p => 'hccg00', :hfr085p => 'hfra00', :hla020p =>
'hblw00', :hlb020p => 'hblw00', :hlr036p => 'hlr000', :hltc20p =>
'hblw00', :hpr042p => 'hppr00', :hrr081p => 'hrr000', :ierc77p =>
'iner00', :ierc78p => 'iner00', :itc010p => 'itr000', :itc101p =>
'itr000', :jsc047p => 'josh00', :lrc013p => 'lrr000', :lrc096p =>
'lrwk00', :lrfc74p => 'lrr000', :lrfl21p => 'lrr000', :lrgc68p =>
'lrr000', :lrnm21p => 'lrr000', :lrrc21p => 'lrr000', :lrrc22p =>
'lrr000', :lrrc24p => 'lrr000', :lrrc37p => 'lrr000', :lrrc49p =>
'lrr000', :lrrc74p => 'lrr000', :lrrc82p => 'lrr000', :lrrmc6p =>
'lrr000', :lrrt37p => 'lrr000', :lrt013p => 'lrr000', :lrtc68p =>
'lrr000', :lwc001p => 'lw0000', :lwc002p => 'lw0000', :lwca85p =>
'lw0000', :lwgc86p => 'lw0000', :lwsc85p => 'lw0000', :mal058p =>
'malr00', :mcc019p => 'mcr000', :mopc68p => 'mopc00', :mopd68p =>
'mopc00', :mopec8p => 'mopc00', :mopt68p => 'mopc00', :mpc067p =>
'mopc00', :mrl150p => 'mrlr00', :osbc35p => 'oshr00', :osc022p =>
'oshr00', :pec029p => 'pen000', :pgc116p => 'pygd00', :pgc117p =>
'pygd00', :plr126p => 'plir00', :psc044p => 'pslr00', :pslc45p =>
'pslr00', :psvc45p => 'pslr00', :ptcc54p => 'ptcj00', :pvl162p =>
'pvlr00', :rtc129p => 'rkpg00', :src087p => 'srlr00', :tlc100p =>
'txlr00', :tmc030p => 'tmeg00', :tmc032p => 'tmwr00', :tmc040p =>
'tmf000', :tmc045p => 'tmtr00', :tmc050p => 'tmfp00', :tmc060p =>
'tmcp00', :tmc065p => 'tmpl00', :tmc070p => 'tmeg00', :tmc080p =>
'tmi000', :tmc090p => 'tmre00', :tmc100p => 'tm0000', :tmcc20p =>
'tmcp00', :tmf186p => 'tmf000', :tmi185p => 'tmus00', :tmic20p =>
'tmus00', :tmrc20p => 'tmre00', :tms190p => 'stat00', :tmsp50p =>
'stat00', :tmt055p => 'tmtr00', :uln007p => 'ulr000', :wcr006p =>
'wcr000', :whl164p => 'wplr00', :ad137b1p => 'lrr000', :ad137b2p =>
'lrr000', :adc095 => 'lrr000', :eb062b1p => 'ebcd00', :eb161b1p =>
'ebcd00', :eb161b2p => 'ebcd00', :eb161b4p => 'ebcd00', :eb960b1p =>
'ebcd00', :eb960b2p => 'ebcd00', :er022b1p => 'er0000', :er022b2p =>
'er0000', :er121b1p => 'er0000', :er121b2p => 'er0000', :er121b5p =>
'er0000', :er901b1p => 'er0000', :er901b2p => 'er0000', :er901b3p =>
'er0000', :fe015b1p => 'lrr000', :ie005b1p => 'lrr000', :ie105b1p =>
'lrr000', :it071b1p => 'itr000', :it171b1p => 'itr000', :it171b3p =>
'itr000', :lr315b1p => 'lrr000', :lr315b2p => 'lrr000', :lrr084 =>
'lrr000', :lrr089 => 'lrr000', :lrr090 => 'lrr000', :lrr095 => 'lrr000',
:lrr096 => 'lrr000', :lrr122 => 'lrr000', :ml032b1p => 'med000',
:ml032b1t => 'med000', :ml032b2p => 'med000', :ml032b2t => 'med000',
:ml131b1p => 'med000', :ml131b1t => 'med000', :ml131b2t => 'med000',
:ml930b1p => 'med000', :ml930b1t => 'med000', :os042b2p => 'oshr00',
:os142b2p => 'oshr00', :osh034 => 'osh200', :pq181b1p => 'pq0000',
:pq181b1t => 'pq0000', :pq181b2p => 'pq0000', :pq181b3t => 'pq0000',
:pq192b1p => 'pq0000', :pq192b1t => 'pq0000', :pq192b3p => 'pq0000',
:wh650b1p => 'lrr000', :wh650b2p => 'lrr000 }

4 psfiles = Dir.glob("*.ps")
5 psfiles.each do |psfile|
6 filehash.each { |key, value| puts "#{value}" if
File.fnmatch('#{key}*', #{psfile}) }
7 end

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

20 Answers

Jeff Schwab

8/14/2006 4:51:00 PM

0

Peter Bailey wrote:

> Error:
> test2.rb:7: parse error, unexpected kEND, expecting '}'

> File.fnmatch('#{key}*', #{psfile}) }

Put #{psfile} in double-quotes.

James Gray

8/14/2006 5:03:00 PM

0

On Aug 14, 2006, at 11:42 AM, Peter Bailey wrote:

> Hello,

Hello.

> I get the following error message from RUBY when I try to run the
> script
> shown below. Sorry, the hash is huge.

Just FYI, the language is Ruby, not RUBY. ;)

I found two problems with your code...

> :wh650b1p => 'lrr000', :wh650b2p => 'lrr000 }

That very last Hash entry is missing a closing quote.

> File.fnmatch('#{key}*', #{psfile}) }

I'm not 100% sure what was intended here, but the # character starts
a comment, causing the rest of the line, including a needed ) and },
to be ignored.

Hope that helps.

James Edward Gray II

Gary Wright

8/14/2006 5:05:00 PM

0


On Aug 14, 2006, at 12:42 PM, Peter Bailey wrote:
> File.fnmatch('#{key}*', #{psfile})

Your quotes are wrong. I think this is what you are looking for:

File.fnmatch("#{key}*", psfile)

Ara.T.Howard

8/14/2006 5:12:00 PM

0

Jeff

8/14/2006 6:13:00 PM

0

You're missing a closing quote in the last value of the hash:

> :wh650b1p => 'lrr000', :wh650b2p => 'lrr000 }

should be :

:wh650b1p => 'lrr000', :wh650b2p => 'lrr000' }

Jeff
softiesonrails.com


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

Peter Bailey

8/14/2006 6:37:00 PM

0

Jeff Cohen wrote:
> You're missing a closing quote in the last value of the hash:
>
>> :wh650b1p => 'lrr000', :wh650b2p => 'lrr000 }
>
> should be :
>
> :wh650b1p => 'lrr000', :wh650b2p => 'lrr000' }
>
> Jeff
> softiesonrails.com

Thanks, Jeff. I did find that problem already and I fixed it. I also had
a typo in filehash.each. It should be fileshash.each. Fixed that, too.
But, anyway, it still ain't working. I'm getting the following error:

test2.rb:9: parse error, unexpected kEND, expecting $

where line 9 is now the last line of the script, the "end" line.



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

Jeremy Tregunna

8/14/2006 6:40:00 PM

0


On 06-08-14, at 12:42, Peter Bailey wrote:

> Hello,
> I get the following error message from RUBY when I try to run the
> script
> shown below. Sorry, the hash is huge.
>
> I need to match the beginning of file names with the keys in a
> hash. If
> there's a match, then I need to pull the value for that matching key.
> That's all really.
>
> Error:
> test2.rb:7: parse error, unexpected kEND, expecting '}'
>
> [snip]
>
> 4 psfiles = Dir.glob("*.ps")
> 5 psfiles.each do |psfile|
> 6 filehash.each { |key, value| puts "#{value}" if
> File.fnmatch('#{key}*', #{psfile}) } # <-- that last } should be an
> end, or the do above should be a {

--
Jeremy Tregunna
jtregunna@blurgle.ca


"One serious obstacle to the adoption of good programming languages
is the notion that everything has to be sacrificed for speed. In
computer languages as in life, speed kills." -- Mike Vanier


Jeremy Tregunna

8/14/2006 6:48:00 PM

0


On 06-08-14, at 14:39, Jeremy Tregunna wrote:

>
> On 06-08-14, at 12:42, Peter Bailey wrote:
>
>> Hello,
>> I get the following error message from RUBY when I try to run the
>> script
>> shown below. Sorry, the hash is huge.
>>
>> I need to match the beginning of file names with the keys in a
>> hash. If
>> there's a match, then I need to pull the value for that matching key.
>> That's all really.
>>
>> Error:
>> test2.rb:7: parse error, unexpected kEND, expecting '}'
>>
>> [snip]
>>
>> 4 psfiles = Dir.glob("*.ps")
>> 5 psfiles.each do |psfile|
>> 6 filehash.each { |key, value| puts "#{value}" if
>> File.fnmatch('#{key}*', #{psfile}) } # <-- that last } should be
>> an end, or the do above should be a {

Bah disregard, still sleepy. Sorry for the noise.

--
Jeremy Tregunna
jtregunna@blurgle.ca


"One serious obstacle to the adoption of good programming languages
is the notion that everything has to be sacrificed for speed. In
computer languages as in life, speed kills." -- Mike Vanier


Peter Bailey

8/15/2006 12:56:00 PM

0

unknown wrote:
> On Tue, 15 Aug 2006, Peter Bailey wrote:
>
>> Hello,
>> I get the following error message from RUBY when I try to run the script
>> shown below. Sorry, the hash is huge.
>>
>> I need to match the beginning of file names with the keys in a hash. If
>> there's a match, then I need to pull the value for that matching key.
>> That's all really.
>>
>> Thanks,
>> Peter
>

> harp:~ > cat a.rb
>
> require 'fileutils'
> require 'yaml'
>
> fileshash = YAML.load DATA.read
>
> Dir.chdir 'u:/indexes'
>
> paths = Dir.glob "*.ps"
>
> paths.each do |path|
> filehash.each{|k,v| puts "#{ v }" if File.fnmatch("#{ k }*", "#{ path
> }") }
> end
>
> __END__
> ---
> aacmc7p: aacm00
> acm059p: bacm00
> acm061p: bacm00
> acmc59p: bacm00
> acmt59p: bacm00
> adc064p: adam00
> adm064p: adam00
> adm065p: adam00
> apr005p: tmap00
> atc071p: atrr00
> bac054p: bnkr00
> bkc031p: bblr00
> bpc061p: btm000
> cal158p: clas00
> car125p: care00
> cbncc7p: cbnc00
> cgmprp_: cglw00
> chc074p: chem00
> chc075p: chem00
> clc017p: clr000
> crc074p: crl000
> ctl157p: ctlr00
> dec079p: der000
> dlc080p: dlr000
> dtc069p: dt0000
> edc131p: eddg00
> edr094p: edr000
> edt131p: eddg00
> eeomc7p: eeom00
> eip066p: eplr00
> emgc15p: emgd00
> erc023p: er0000
> ercr25p: er0000
> exe155p: exer00
> fcc093p: fcr000
> flc046p: flr000
> gec099p: gerr00
> hac076p: hzrf00
> hcc201p: hccg00
> hcci01p: hccg00
> hcp030p: hce000
> hct201p: hccg00
> hcti01p: hccg00
> hfr085p: hfra00
> hla020p: hblw00
> hlb020p: hblw00
> hlr036p: hlr000
> hltc20p: hblw00
> hpr042p: hppr00
> hrr081p: hrr000
> ierc77p: iner00
> ierc78p: iner00
> itc010p: itr000
> itc101p: itr000
> jsc047p: josh00
> lrc013p: lrr000
> lrc096p: lrwk00
> lrfc74p: lrr000
> lrfl21p: lrr000
> lrgc68p: lrr000
> lrnm21p: lrr000
> lrrc21p: lrr000
> lrrc22p: lrr000
> lrrc24p: lrr000





Wow. Thanks, unknown Guest. I didn't do your YAML thing, but, I did use
your last fileshash.each routine, and that worked beautifully. I had
actually written a note to the forum a couple of weeks ago regarding
YAML, because, intuitively, it seemed to make sense for such a large
piece of data like mine. Now, forgive my ignorance, but, is your YAML
table set up in a separate file, or, is it literally in the same script?
I would like to know more about YAML.

Thanks.

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

Peter Bailey

8/15/2006 12:56:00 PM

0

unknown wrote:
> On Tue, 15 Aug 2006, Peter Bailey wrote:
>
>> Hello,
>> I get the following error message from RUBY when I try to run the script
>> shown below. Sorry, the hash is huge.
>>
>> I need to match the beginning of file names with the keys in a hash. If
>> there's a match, then I need to pull the value for that matching key.
>> That's all really.
>>
>> Thanks,
>> Peter
>

> harp:~ > cat a.rb
>
> require 'fileutils'
> require 'yaml'
>
> fileshash = YAML.load DATA.read
>
> Dir.chdir 'u:/indexes'
>
> paths = Dir.glob "*.ps"
>
> paths.each do |path|
> filehash.each{|k,v| puts "#{ v }" if File.fnmatch("#{ k }*", "#{ path
> }") }
> end
>
> __END__
> ---
> aacmc7p: aacm00
> acm059p: bacm00
> acm061p: bacm00
> acmc59p: bacm00
> acmt59p: bacm00
> adc064p: adam00
> adm064p: adam00
...


Wow. Thanks, unknown Guest. I didn't do your YAML thing, but, I did use
your last fileshash.each routine, and that worked beautifully. I had
actually written a note to the forum a couple of weeks ago regarding
YAML, because, intuitively, it seemed to make sense for such a large
piece of data like mine. Now, forgive my ignorance, but, is your YAML
table set up in a separate file, or, is it literally in the same script?
I would like to know more about YAML.

Thanks.

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