mrkode@gmail.com
12/9/2005 7:25:00 PM
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 = "9"
=> "9"
irb(main):024:0> a.succ!
=> "10"
On 12/9/05, Logan Capaldo <logancapaldo@gmail.com> 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 = {};
> > 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
>
>
>