[mplspm]: In a sorted list [better]

Thomas Eibner thomas at stderr.net
Fri Mar 22 09:48:04 CST 2002


On Fri, Mar 22, 2002 at 11:40:45AM -0600, Tim Burlowski wrote:
> OK, well I missed the requirements again, as you don't want a count only
> yes or no. Doooh. I am a double dumbass today. How about this?
> 
> 
> #!/usr/bin/perl
> my @list=(1,2,3,4,1,2,3,4,5,6,"string","a","string");
> my %count = ();
> my $i = 0;
> $count{$_}++ for (@list);
> my $bool = "no";
> CHECK: foreach (keys %count) {
>     $i++; 
>     if (/string/){
>         $bool = "yes";
>         last CHECK;
>     }    
> }
> print "$bool, string found in list, $i iterations";

If he just wants to know if it's in there or not why go through the first
for loop and consume that memory? (After all he says up to 2 million
strings)

my $found = 0;
CHECK: for (@list) {
    if ($_ eq 'string') {
        $found = 1;
        last CHECK;
    }
}

-- 
  Thomas Eibner <http://thomas.eibner.dk/> DnsZone <http://dnszone.org/>
  mod_pointer <http://stderr.net/mod_pointer> <http://photos.eibner.dk/>
  !(C)<http://copywrong.dk/>                  <http://apachegallery.dk/>
          Putting the HEST in .COM <http://www.hestdesign.com/>


--------------------------------------------------
Minneapolis Perl Mongers mailing list

To unsubscribe, send mail to majordomo at pm.org
with "unsubscribe mpls" in the body of the message.



More information about the Mpls-pm mailing list