From: JB Eriksson Date: 2005-12-10T04:24:43+09:00 Subject: Re: new to Ruby - pls help in translating this ------=_Part_10186_18470900.1134156277131 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline well, there is the succ method. and there's a succ! on String, which could work in this case, I guess(?), since: irb(main):023:0> a =3D "9" =3D> "9" irb(main):024:0> a.succ! =3D> "10" On 12/9/05, Logan Capaldo wrote: > > > 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 =3D {}; > > while (<>) > > { > > chomp; > > $hash{$_}++; > > } > > > > foreach my $key (sort keys %hash) > > { > > print "$key: $hash{$key}\n" if ($hash{$key} > 1); > > } > > > > hash =3D Hash.new { |h, k| h[k] =3D 0 } > > while gets > $_.chomp! > hash[$_] +=3D 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 =3D 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 > > > ------=_Part_10186_18470900.1134156277131--