From: Tim Hammerquist Date: 2005-12-10T07:04:57+09:00 Subject: Re: new to Ruby - pls help in translating this Sam Dela Cruz wrote: > 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 = {}; That doesn't do what you think it does. In Perl, the {} here will create a hash reference, which is a scalar datatype. You're assigning it to a %hash variable, which accepts LIST data. This ends up being the same as: %hash = ( {} ); Which creates a hash named %hash, stringifies the hash reference and uses it as a key, and without any additional values to work with, uses undef as a value for the key. To see this in action: use Data::Dumper; my %hash = {}; print Dumper( \%hash ); Compare to: use Data::Dumper; my %hash = (); print Dumper( \%hash ); Yes. You want to use parentheses. > while (<>) > { > chomp; > $hash{$_}++; > } > > foreach my $key (sort keys %hash) > { > print "$key: $hash{$key}\n" if ($hash{$key} > 1); > } HTH, Tim Hammerquist