On 12/9/05, Corey Jewett <corey / syntheticplayground.com> wrote:
> Or, in keeping with the DRY and KISS principles I humbly submit the
> lowly shell command, uniq, instead of Ruby at all:
>
> $ uniq -d <file>
>
> e.g.
>
> /tmp corey$ cat foo
> 1
> 1
> 2
> 3
> 3
> 4
> /tmp corey$ uniq -d foo
> 1
> 3
> /tmp corey$
>

but it doesn't always work:
pate@linux:~/scratch> cat nums
1
2
3
4
4
5
6
5
7
pate@linux:~/scratch> uniq -d files
uniq: files: No such file or directory
pate@linux:~/scratch> uniq -d nums
4
pate@linux:~/scratch>


you need to do:
pate@linux:~/scratch> sort -n nums | uniq -d
4
5


but if this is supposed to be part of a larger program, shell
is probably the wrong way to go about it.


> Although, it was interesting to see the diversity of Ruby solutions.
>
> Corey
>
>
> On Dec 9, 2005, at 10:48 AM, 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
>
>
>


--
thanks,
-pate
-------------------------