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


In reply to Re: Closest match Display by tachyon
in thread Closest match Display by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.