Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I am working with perl manipulating a database and I have to build a search scheme that as you starting to enter the name of the location in a text box field it would right way display (print) underneath, the closest mach to what I am typing in that text box, and I confess that I am lost on this one. Can anyone help me with that?

Replies are listed 'Best First'.
Re: Closest match Display
by freddo411 (Chaplain) on Oct 07, 2003 at 18:16 UTC
    Forgive me for not taking you literally, but I imagine what you are describing is really "search completion", similar to command line completion in a shell or URL completion in a browser. The purpose is to save the user typing by providing an intellegent guess (based upon some algorythm) of what the user is typing and providing a short-cut key to "complete" the entry.

    If you wanted to do implement this you have know in advance the complete pool of expected text entries. In our example of command line completion, that would be all possible executable files in the path. In the browser example, that's usually the history list of sites visited before.

    This list is then put (in advance for speed reasons) into a alphabetic tree data structure. (Google for details on tree structures... this is beyond the scope of this dicussion).

    At the time of user input a very fast lookup can be performed on the tree structure to find the remaining possible entries if any. Depending on your UI, you can put one of these in front of the user, or you can display the whole list in some way (shells use the tab key to either complete the entry if unique or show a list if not).

    In the perl realm, this Module looks relavent: Term::Complete

    -------------------------------------
    Nothing is too wonderful to be true
    -- Michael Faraday

Re: Closest match Display
by CombatSquirrel (Hermit) on Oct 07, 2003 at 17:22 UTC
Re: Closest match Display
by tachyon (Chancellor) on Oct 08, 2003 at 01:46 UTC

    What you want is really javascript if you mean do it in the browser. As noted you need a SORTED array of all the possiblities. Then you would do something like:

    <head> <script> var elements = 6; options = new Array(); options[0] = 'h'; options[1] = 'he'; options[2] = 'heat'; options[3] = 'hell'; options[4] = 'hello'; options[5] = 'hello world'; function inputDelta() { var index = -1; var search = someform.search.value; if ( search == '' ) { someform.guess.value = search; return 1; } var re = new RegExp(search); for( i = 0; i < elements; i++ ) { if ( options[i] == search ) { break; } if ( options[i].match(re) ) { index = i; } } if ( index != -1 ) { someform.guess.value = options[index]; } else { someform.guess.value = search; } return 1; } </script> </head> <body onLoad="javascript:someform.search.select();someform.search.focu +s()"> <form name="someform"> <input type="text" name="search" value="Type Search Here" onChange="inputDelta()" onKeyup="inputDelta()" onBlur="inputDelta()"> +<br> <input type="text" name="guess" value=""> </form>

    Note that this is a horrendously inefficient algorithm in that it iterates through a sorted list every time. See File::SortedSeek for a Perl implementation of the binary tree/split the difference algorithm. Have fun implementing it in javascript! If the list of possibilities is large you would put it in a .js file and include it to save the download overhead (if you have repeat access issues)

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Closest match Display
by Yendor (Pilgrim) on Oct 07, 2003 at 17:51 UTC