[lug] perl :)

rm at fabula.de rm at fabula.de
Mon Aug 19 06:20:10 MDT 2002


On Sun, Aug 18, 2002 at 09:28:05PM +0000, j davis wrote:
> 
> so if syntax was changed to <3 then it would be ok...right?

No, with code like this you can only test _if_ you have seen
an element, but not how many times an element occurs in a list.
Think of it like this: to answer the question "Have i allready seen
this element" you only need to keep track of the _names_ of the 
elements (that's what you do by incrementing $count{$_}. To answer
this question the value  of '$count{$_}' doesn't matter. If it's in
the list at all you've seen it). If you ask "is this element n times
in the list" you need to count the occurence of the elementin the
list (this is what some other posters suggested, after you passed the
whole list through the grep %count will hold the exact number of
occurances for each element. Only now can you look up the elements
that occure n times. Or think of the grep part as a line at the
cash register. As the person at the cash register you can easily tell
if someone allready bought ChocolateFrosties today, but you can't tell
at 11.00 o'clock whether three people will by that item today.


 Hth 

   Ralf

> jd
> 
> >From: rm at fabula.de
> >Reply-To: lug at lug.boulder.co.us
> >To: lug at lug.boulder.co.us
> >Subject: Re: [lug] perl :)
> >Date: Sun, 18 Aug 2002 21:28:11 +0200
> >
> >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
> >_______________________________________________
> >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
> 
> _________________________________________________________________
> Chat with friends online, try MSN Messenger: http://messenger.msn.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