[lug] perl :)

Rob Judd rjudd at mlug.missouri.edu
Fri Aug 16 10:05:51 MDT 2002


> you can't use this trick to find those that appear exactly twice or
> thrice; it works for "first appearance" because you are dealing with
> cardinal numbers, which can't go negative.
>
> to find elements that appeared a certain number of times, i'd probably
> do something like this:
>
>   my @appears_twice = do {
>       my %count;
>       foreach my $elt (@list) { ++$count{$_} }
>       grep { $count{$_} == 2 };
>   }

Surely you can use something similar ... won't this work

@twice = grep { ++$count{$_} == 2 } qw(a b a c d d e f g f h h);

to give @twice = qw(a d f h) ?

Cheers, Rob




More information about the LUG mailing list