--nextPart2593319.70Ojd7Xiaq
Content-Type: text/plain;
charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
Hey,
after looking at your code (wich was a bit hard, since i don't speak perl) =
=2D i=20
came up with following solution, that doesn't seem very ruby-esque but work=
s=20
well for me.
I'm sure someone brings the code down to one line, but i'm a bit too lazy t=
o=20
try that
file =3D File.open(ARGV[0]).readlines
h =3D {}
file.each{|x| (h[x].nil?) ? (h[x] =3D 1) : (h[x] =3D h[x]+=3D1)}
h.each_pair{|k,v| puts "#{k.strip} appears #{v} times" if v >=3D 2}
Am Freitag, 9. Dezember 2005 19:48 schrieb Sam Dela Cruz:
> 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 =3D {};
> 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
--nextPart2593319.70Ojd7Xiaq
Content-Type: application/pgp-signature
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
iD8DBQBDmdd8MdQeL6eBxhIRAjkCAJ9Xl6Gcu+jPkn+Kkaksxz LLd8yhywCg0dkd
RpwC7Xt6ZUMuJKk8SEfxaAU=
=xeN0
-----END PGP SIGNATURE-----
--nextPart2593319.70Ojd7Xiaq--