From: John Rubinubi Date: 2000-12-23T14:02:54+09:00 Subject: [ruby-talk:7990] Re: beginner's question -- hashes Thanks very much, David. I wasn't sure if I had some silly syntax error. Unfortunately I accidentally saved something else (the console window in rubywin) over my code so I wasn't able to figure out what my error was. I tried to recreate it but this time it all worked according to plan. Funny, I was disappointed when it worked. At the end of this message, after the quoted stuff, I have appended the first program written by my 13 year old son, (which does essentially the same thing I was trying to do.) I'm not sure if such inclusion is appropriate for this list/group but I'm so proud of him I can't help myself. It uses the free cue-cat scanner you can get from radio shack. You scan bar-codes into a hash as keys and associate them with names. Not particularly useful but it sure has been a big victory for him. John On Fri, 22 Dec 2000, David Alan Black wrote: > Hello -- > > On Fri, 22 Dec 2000, John Rubinubi wrote: > > > when I loop through the following... > > > > st=gets.chop! > > if not hash[st] #I suppose I should use has_key? > > hash[st]='some_value' > > else > > puts hash[st] > > end > > > > > > when I go through the loop again and enter the same key it doesn't find it > > although the same code finds the key-value pairs that I put in the hash > > with a literal when I created it. > > I can't duplicate your problem: > > hash = {} > loop do > st=gets.chop! > if not hash[st] > hash[st]='some_value' > else > puts hash[st] > end > end > > run: > > a > b > a > some_value > b > some_value > > > David > > -- > David Alan Black > home: dblack@candle.superlink.net > work: blackdav@shu.edu > Web: http://pirate.shu.edu/~blackdav > > #by: Jesse scan={} done=false while not done puts "scan an item, enter q to quit, or p to print your items" g=gets.chop! if g=='q' done=true elsif g=='p' scan.each_value {|value| puts value } elsif not scan.key?(g) puts 'new item -- enter name' ge=gets.chop! scan2={g=>ge} scan.update(scan2) else puts scan[g] end end puts"\nHasta Luego!!"