[mplspm]: In a sorted list [better]

Jim Anderson jim at acadcam.com
Fri Mar 22 09:40:54 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";

So what's the point of spending all that time building a hash, when the
array was already sorted???

> > > Previously Josh Aas(josha at mac.com) wrote: 
> > > 
> > > > Hey MPM,
> > > >     If I have an alphabetically sorted array of strings (containing up to 2
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

> > > > million strings), and I want to find out if any strings in that array equal

And with 2 million strings, building a hash is a non-trivial amount of
time.

And instead of that great big foreach loop, why not just use

print "Found it\n" if defined($count{"string"});

-- 
Jim Anderson			(612) 782-0456     jim at acadcam.com
Anderson CAD/CAM, Inc		Lucifer designed MS-DOS to try
3800 Apache Lane NE		    men's souls.
St Anthony, MN  55421		Then he had a better idea...


--------------------------------------------------
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