-----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 at http://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 <mike / stok.ca> 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-----