Shop OBEX P1 Docs P2 Docs Learn Events
Using Text as a variable — Parallax Forums

Using Text as a variable

Joe FishbackJoe Fishback Posts: 99
edited 2006-11-13 23:00 in BASIC Stamp
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-

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2006-11-13 20:14
    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 SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-11-13 21:04
    Joe,
    ·
    ·· 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:
    DATA "PASSWORD", 0
    

    You could do something like:
    DATA "NAME", 0
    DATA "ANOTHER NAME", 0
    DATA "AGAIN", 0
    

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

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2006-11-13 21:56
    Joe,

    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
    
    DO
      name = 0
      DO
        DEBUGIN char
        IF char = CR THEN EXIT
        IF char >= "a" AND char <= "z" THEN char = char - $20
        IF char >= "A" AND char <= "Z" THEN name = name * 887 + char
      LOOP
      DEBUG DEC name, CR
    LOOP
    
    
    


    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
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2006-11-13 23:00
    I downloaded a list of nearly 5500 American first names from the U.S. Census Bureau 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.

    -Phil
Sign In or Register to comment.