From: RichardOnRails Date: 2011-05-09T12:10:26+09:00 Subject: Re: Must be my bug, but it looks like Ruby's ... could it be? On Apr 30, 9:14 am, Mike Stok wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > On 2011-04-30, at 12:25 AM, RichardOnRails wrote: > > > > > > > > > > > I know my Subject line looks like heresy in this newsgroup, but I > > can't figure out how I'm screwing up in this bug. > > > I posted the program athttp://www.pastie.org/1849430 > > It reads in a little control data, which is displayed at > >http://www.pastie.org/1849434 > > > The program reads in the control data and stores it in hash containing > > two hashes in this case, which represents the two pseudo-hashes in the > > control data. > > > My problem is conflicting output by the statements numbered 155 & 156: > >      puts %[%d\t%s => %s] % [num+=1, key, value] > >      print " key = "; p key > > which respectively yield the following as the 4th and 5th output lines > > of the program: > >      1      BackupFile => {:filename => "BackupFile.rb", :path => > > ".", :digits => 3} > >       key = " BackupFile => {:filename => \"BackupFile.rb\", :path => \". > > \", :digits" > > > The first of these output lines indicates to me that key == > > "BackupFile", as I expected. > > But the second line seems to indicate that key == " BackupFile => > > {:filename => \"BackupFile.rb\", :path => \".\", :digits" > > > Can anyone suggest how I can end this inconsistency? > > I don't see an inconsistency in the output, the data might not have been split as you expected. > > #!/usr/bin/env ruby > > num = 0 > key = ' BackupFile => {:filename => "BackupFile.rb", :path => ".", :digits' > value = '3}' > > puts %[%d\t%s => %s] % [num+=1, key, value] > print " key = "; p key     > > seems to produce approximately your output, in your first puts how do you tell which => is which? > > Your split looks like the first .+ is too greedy as it tries to consume as much of the string as possible while the match can still work.  You could use the ? modifier to make it parsimonius, or use split maybe: > > #!/usr/bin/env ruby > > string = 'BackupFile => {:filename => "BackupFile.rb", :path => ".", :digits => 3}' > > string =~ /(^.+)\s\=\>\s(.+)$/ > p $1 > p $2 > > string =~ /(^.+?)\s\=\>\s(.+)$/ > p $1 > p $2 > > array =  string.split(' => ', 2) > p array[0] > p array[1]   > > => > > ratdog:tmp mike$ ./try.rb > "BackupFile => {:filename => \"BackupFile.rb\", :path => \".\", :digits" > "3}" > "BackupFile" > "{:filename => \"BackupFile.rb\", :path => \".\", :digits => 3}" > "BackupFile" > "{:filename => \"BackupFile.rb\", :path => \".\", :digits => 3}" > > Hope this helps, > > Mike > > - -- > > Mike Stok http://www.stok.ca/~mike/ > > The "`Stok' disclaimers" apply. > > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.4.11 (Darwin) > > iEYEARECAAYFAk28C0EACgkQnsTBwAWZE9qAgACdGxPswALo1oYNerDr/kjElDyz > gqQAn2G3hUKdMLZpcuScqE7kLZ/W/aLR > =UJYj > -----END PGP SIGNATURE----- Hi Mike, Thanks for your response. I had more serious things to work on last week; hence, my belated response. Your question about the appropriateness of my regexp got me to look more closely at my parsing of the input file and saw an incorrect result. The fact that one use of the incorrect data misled me to "cry wolf." I'll try to apply the Abstract Syntax Tree pattern from Russ Olsen's "Design Patterns in Ruby". It's perhaps overkill for this problem but it's better than my hand-crafted incorrect routine. Best wishes, Richard