Table of Contents
# $EPIC: prefix.txt,v 1.4 2006/08/19 04:05:34 sthalik Exp $
Synopsis:
$prefix(<word list>)
Technical:
- If the <word list> argument is omitted the empty string is returned.
- The return value of this function is whatever string is the common initial substring (CIS) of all of the words in <word list>. If there is no CIS in <word list>, the empty string is returned.
- This function uses an O(M*N) algorithm where M is the number of words in <word list> and N is the length of the first word in <word list>. Take this into consideration when planning.
Practical:
This function can be used to do pseudo-completion. To explain, consider if you are working on a tab completion script and the user types “foo” and presses tab. Normally you would extract from $onchannel() everything that matched “foo*”, but what if that returns more than one word? It is considerate to the user to complete as much as possible for them and then prompt them for the rest. So let's say that you had two users, “foobar” and “foobaz”. When the user pressed <tab> after typing “foo” you would do something like this:
# Some code goes here to put "foo" in $input. @ users = filter($input* $onchannel()) @ result = prefix($users) if (numwords($users) > 1) { beep } # Some code goes here to remove the 'foo' from the input # line and paste $result in its place.
$result will contain “fooba” because “fooba” is the CIS of the word list (foobar foobaz). So when the user presses <tab> you will put the string “fooba” in place of “foo” and beep to let them know that it is not an exact match. You could use $users to tell them what their options are, if you wanted to.
If the user typed something like “boo” and there were no users on the channel whose nickname started with “boo”, then $result would be the empty string. You could trap that as well and give the user an error message.
Returns:
The Common Initial Substring (CIS) of all of the words in <word list>
Examples:
$prefix(foobar foobaz foobooya) returns "foob" $prefix(one two three) returns the empty string.