From: Logan Capaldo Date: 2005-12-10T04:15:59+09:00 Subject: Re: new to Ruby - pls help in translating this On Dec 9, 2005, at 1:48 PM, Sam Dela Cruz wrote: > Hi, > > I'm starting to use Ruby in one of my projects at work. I'm coming > from a > Perl background. > In my project I would need to parse a list of numbers (thousands of > them) > and then return the duplicates. In perl, I can do this: > > ##### Perl code > %hash = {}; > while (<>) > { > chomp; > $hash{$_}++; > } > > foreach my $key (sort keys %hash) > { > print "$key: $hash{$key}\n" if ($hash{$key} > 1); > } > hash = Hash.new { |h, k| h[k] = 0 } while gets $_.chomp! hash[$_] += 1 end > I tried to translate this in Ruby, but could not find en equivalent of > $hash{$_}++, this is auto increment. Ruby has no auto-increment since variables are more like labels on objects than containers for objects and numbers are generally immutable in ruby. IOW: x = 3 x++ in ruby would be like typing 3++ which doesn't make sense really. > Can somebody tell me how this is to be done in Ruby? Or maybe the > Ruby > way on how to attack this whole thing. Thanks. > > Regards, > Sam