From: Wilson Bilkovich Date: 2005-12-10T04:37:26+09:00 Subject: Re: new to Ruby - pls help in translating this Or, something else to think about.. shamelessly borrowed from this mailing list a month or so ago.. Wrapped in lame 'gets' code to let you play with it at the command line. I presume you'd actually do something cooler with STDIN or a filename. class Array def dups h, d = {}, []; each{|e| h[e] ? (d << h.delete(e)) : (h[e] = e)}; d end end numbers = [] while n = STDIN.gets.chomp break if n == '' numbers << n end puts numbers.dups I love reopening Array. --Wilson. On 12/9/05, 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); > } > > I tried to translate this in Ruby, but could not find en equivalent of > $hash{$_}++, this is auto increment. > 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 >