View Full Version : Using Text as a variable

Joe Fishback
11-14-2006, 03:06 AM
I would like to be able to have someone type in their first name and then store the name typed as a variable. Then the program would compare this name as Text to a variable Text name. If the INPUT matches one listed in the program, the program would go to a sub and do something. Attached is a program I tried, but it does not work. Can anyone help me.

ииииии Joe Fishback

-Robots are my friends-

Mike Green
11-14-2006, 03:14 AM
There are no string comparisons in PBasic. The only built-in string stuff is in the I/O statements. I suggest you define the names you want as DATA statements in your program where they'll be stored in the EEPROM along with your program. Terminate the names with a "*" (to mark the end). You then need to set up a subroutine to compare your input byte array character by character with the information in the DATA statement, copied to a variable with a READ statement. If the two match up through the "*", they're equal. Look at the description of the DATA and READ statements in the PBasic manual. See if you can figure out what this comparison subroutine would look like.

Chris Savage
11-14-2006, 04:04 AM
ии I have posted code a few times for comparing passwords entered from keypads.и That same code could be used as you describe.и The limitations on a BS2 are the amount of available array storage.и On any other BASIC Stamp you can use the SPRAM in the same manner.и This can be compared to the internal values stored in DATA statements terminated with a 0.и For example:


You could do something like:


Then you would have a lookup routine compare character by character until you hit the 0.

Chris Savage
Parallax Tech Support

Phil Pilgrim (PhiPi)
11-14-2006, 04:56 AM

You could try a hash function. What this does is convert a string of characters to a number. Then you can compare the number to other, stored numbers that were obtained in the same way. A word variable can take on 65536 different values. Under ideal circumstances, each of these would be the hash for a different name. So, ideally, two different names will virtually never generate the same number. When this happens, it's know as a "collision", and collisions can and do happen. The idea is to come up with a hash function that minimizes this possibility.

The program below is an example of a hash function. It accepts characters from DEBUGIN until it receives a carriage return. As it's doing so, it converts all lower-case letters to upper-case and filters out any non-letters. Then, it keeps a running computation of the hash in the variable name. Once it receives a carriage return, it spits out the number and starts over.

' {$STAMP BS2}
' {$PBASIC 2.5}

name VAR Word
char VAR Byte

name = 0
IF char >= "a" AND char <= "z" THEN char = char - $20
IF char >= "A" AND char <= "Z" THEN name = name * 887 + char

No attempt has been made to test the collision behavior of this function. The way to do this would be to write a similar program for the PC and feed it a list of first names from the internet and see if any collisions occur. But you can at least try it out with the names you might encounter to see if it works well enough.


Phil Pilgrim (PhiPi)
11-14-2006, 06:00 AM
I downloaded a list of nearly 5500 American first names from the U.S. Census Bureau (http://www.census.gov/genealogy/names/names_files.html) and checked the algorithm above for collisions. There were 200 hash values that had more than one name associated with it. So I tried fiddling with the parameters a bit to see if that could be improved. The best I could come up with is 166, using the following formula:

IF char >= "A" AND char <= "Z" THEN name = name * 1453 + char - 64

Attached is the list of collisions produced by this formula.