[lug] perl :)

rm at fabula.de rm at fabula.de
Sun Aug 18 13:28:11 MDT 2002


On Fri, Aug 16, 2002 at 09:31:00PM +0000, j davis wrote:
> 
> it looks like i only need to change the operator and apperance number
> to tweak this little tid-bit.

No, the code below doesn't do what it's supposed to do.
An item that exists three times in the source list will show
up wrongly in the result list. 

 During the first occurence 
   ++$count{$_} == 2
 will be wrong, hence the item won't make it throught the filter,
 but the second time the item shows up
   ++$count{$_} == 2
 will be true and the item will be added to the result list.
 When the item shows up the third time it won't pass, but that
 doesn't matter, it's allready and wrongly in the result list.
 One could write an @at_least_twice that way, but not the wanted
 filter.

   Ralf

> jd
> 
> >From: Rob Judd <rjudd at mlug.missouri.edu>
> >Reply-To: lug at lug.boulder.co.us
> >To: <lug at lug.boulder.co.us>
> >Subject: Re: [lug] perl :)
> >Date: Fri, 16 Aug 2002 10:05:51 -0600 (MDT)
> >
> >
> >> 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
> >
> >_______________________________________________
> >Web Page:  http://lug.boulder.co.us
> >Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
> >Join us on IRC: lug.boulder.co.us port=6667 channel=#colug
> 
> 
> thanks,
> jd
> 
> jd at taproot.bz
> http://www.taproot.bz
> 
> _________________________________________________________________
> Join the world?s largest e-mail service with MSN Hotmail. 
> http://www.hotmail.com
> 
> _______________________________________________
> Web Page:  http://lug.boulder.co.us
> Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
> Join us on IRC: lug.boulder.co.us port=6667 channel=#colug



More information about the LUG mailing list