[mplspm]: In a sorted list

Thomas Eibner thomas at stderr.net
Thu Mar 21 22:25:27 CST 2002


On Thu, Mar 21, 2002 at 10:07:22PM -0600, Josh Aas 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
> a certain string (yes or no, not how many), what is the fastest way to do
> that search? This seems basic to me, I just can't come up with the answer
> and I have an hour to do so. Thanks a lot!

An "easy" way to do it would simply be to grep on the array, but probably
not the fastest way to do it.

if (grep {/^thestring$/} @sorted_array) { # match! }

But since you say you have it sorted alphabetically, you might want to do
something like taking the number of entries, taking the middle element
out and comparing it by the first letter to see wheter it was close to the
string or not and then continue with moving half way to one side.. just an
idea anyway.

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