Checking for Valid Droid Names

A valid STARWARS droid name consists of a letter, a number followed by a letter and a number (actually, not strictly correct, but it will do for the purposes of this demo, which can be expanded upon to check for valid postcodes, ISBN numbers, etc).

The following function, valid() takes a string containing a droid name and returns TRUE or FALSE depending on whether the input is a valid STARWARS droid name.  Note that BASIC does not have pattern matching as standard, however here we make use of the built-in INSTR function.

Example of the program running.
The work is done by the line:
= -SGNINSTR(alpha$, MID$(aThing$, 1, 1)) * INSTR(numeric$, MID$(aThing$, 2, 1)) * INSTR(alpha$, MID$(aThing$, 3, 1)) * INSTR(numeric$, MID$(aThing$, 4, 1)) )

Looking carefully we have the following construction:

= -sgn( isLetter * isNumber * isLetter * isNumber )

The check, (isLetter, or isNumber) is carried out by the INSTR function.  INSTR takes two parameters, the first is the string to search in, and the second is the target string to find.  INSTR is a function which returns the position of a sub-string within a string, optionally starting the search at a specified place in the string. The leftmost character position is 1. If the sub-string is not found, 0 is returned.

Looking at the first call to isLetter, we have:

 INSTR(alpha$, MID$(aThing$, 1, 1))

Which means, look for the first character in the string aThing$ and see if it is in the string alpha$.  If it is, the INSTR function returns a number corresponding to the position of the character in the list.  For example, looking for "C" will return 3.

The next call is:

INSTR(numeric$, MID$(aThing$, 2, 1))

Which means, look for the second character in the string aThing$ and see if it is in the string numeric$.

This pattern matching continues, looking alternately through the string from position 1 to 4.  If each character is found, the result of multiplication will be a large number.  If any of the checks fails, then the result will be zero.

The function then converts large numbers into TRUE, and zero into FALSE.  If you are still awake you have got a STARWARS droid name validator.  The same trick can be applied to look for valid postcodes, car registration plates, etc, etc.  Careful students should add extra validation to make sure they check for a valid string length before passing the string to the validator ("R2D2rubbish") is not valid, but will still pass the check.

These ARE the droids you are looking for.

Source code and test program:

     REM this program tests for valid STARWARS droid names

     REM test program to test function valid()

       INPUT "Enter droid name : " droid$

       IF FN_valid( droid$ ) THEN
"Valid droid name"
"Not a valid droid name"
droid$ = "STOP"

     DEF FN_valid( aThing$ )
     REM checks whether aThing$ is a valid STARWARS droid name
     REM returns TRUE if it is
     REM and FALSE if it is not
LOCAL alpha$, numeric$ : REM variables used by this function ONLY
     numeric$ = "0123456789"
     REM this bit does all the work
= -SGNINSTR(alpha$, MID$(aThing$, 1, 1)) * INSTR(numeric$, MID$(aThing$, 2, 1)) * INSTR(alpha$, MID$(aThing$, 3, 1)) * INSTR(numeric$, MID$(aThing$, 4, 1)) )