DotDragnet
May 23, 2012, 10:28:28 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: follow us on twitter @dotdragnet
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: seaching a list  (Read 1268 times)
Charisma Bypass
Hero Member
*****
Posts: 556



View Profile Awards
« on: July 22, 2009, 04:13:32 PM »

What would be the best way to search thru a list of stuff and say "got it" or "not", assuming no database involved?

Imagine a list of (let's say) towns and villages in the UK, there's thousands of them!  I need a search facility so that any can search for (let's say) Amblethorpe...and if I got it, it says "got it". Or not.

XML ? 
Logged
Charisma Bypass
Hero Member
*****
Posts: 556



View Profile Awards
« Reply #1 on: October 05, 2009, 12:04:47 PM »

Bumping this backup, as the project is now on.

So, a big long list in a .txt file. Best way to make it searchable?

There are about 51000 entries in the list.
Logged
Jeep Stone
Hero Member
*****
Posts: 908



View Profile WWW Awards
« Reply #2 on: October 05, 2009, 02:07:55 PM »

If you can make it XML, then you can use XSLT to match against nodes/attributes. You just need a little bit of javascript/PHP/whatever you like to perform the transform
Logged

JasonD
Global Moderator
Hero Member
*****
Posts: 547



View Profile Awards
« Reply #3 on: October 05, 2009, 03:07:12 PM »

51000 is nothing. Even a dumb read line by line will take no time at all.

Code:
function haveTown($town)
{
    $got = false;
    $fp = fopen('not-so-big-list-o-stuff.txt', 'rb');
    while ($line = trim(fgets($fp))) {
        if ($line == $town) {
            $got = true;
            break;
        }
    }
    fclose($fp);
    return $got;
}
Logged
Charisma Bypass
Hero Member
*****
Posts: 556



View Profile Awards
« Reply #4 on: October 08, 2009, 01:31:57 PM »

Cheers Guys.  Appreciate the time.

Jason, is 51000 entries -seriously- not going to impact the performance of a search?
Logged
JasonD
Global Moderator
Hero Member
*****
Posts: 547



View Profile Awards
« Reply #5 on: October 08, 2009, 03:00:01 PM »

I'd guesstimate (on past performance) the above will take < 0.05 seconds to read to the bottom of the list, assuming it didn't match eariler.

I had a discussion with someone about similar code a few years back with benchmarks and such and we concluded anything under 300,000 lines wasn't worth the effort of doing any different.

You could optimise it further one of two ways, since the further down the list you go the longer it takes sort the list in order of popularity, but I don't have any clever ways to determine that.

Or by sorting the list alphabetically and adding
Code:
if ($line > $town) {
     break;
}

Then if Abingdon isn't on the list it will stop at Accrington instead of reading to York.

If you want more then have some precomputed points for the first entry for the inital letter and fseek to it.

But seriously, any more complexity that that you might as well put it in a database.
Logged
Charisma Bypass
Hero Member
*****
Posts: 556



View Profile Awards
« Reply #6 on: October 08, 2009, 05:18:34 PM »

Many thanks smile
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF | SMF © 2006-2008, Simple Machines Valid XHTML 1.0! Valid CSS!