Shop OBEX P1 Docs P2 Docs Learn Events
string comparison — Parallax Forums

string comparison

ArchiverArchiver Posts: 46,084
edited 2002-02-28 12:59 in General Discussion
Hi

Is there any way to compare a string with a variable in PBASIC?

the following is my code:


serin 1, 1021,[noparse][[/noparse]WAIT(".1"), STR data_in\5]

if data_in = "fwd1z" then FORWARD

if data_in = "rgt1z" then RIGHT

if data_in = "lft1z" then LEFT

if data_in = "rev1z" then REVERSE

if data_in = "stp1z" then STOP

if data_in = "sen1z" then SENSOR


Now, is there any easier way then having to access each array element
and comparing it?

Debu

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2002-02-28 12:59
    You can't directly compare strings. However, you could use LOOKUP to
    help you out:

    For i=0 to 4
    LOOKUP i,[noparse][[/noparse]"f","w","d","1","z"],tst
    if data_in[noparse][[/noparse]i]<>tst then nogo1
    Next
    Equalt_to_fwd1z:
    ' do whatever
    Goto somewhere_else

    Nogo1:
    ' do some more tests

    This gets tedious after awhile. You could put your strings in EEPROM
    using DATA and then use READ, so you could make a subroutine:

    Match:
    match=0 ' assume no match
    ctest=mystrings-5
    Matchtop:
    ctest=ctest+5 ' next string
    for i=0 to 4
    read ctest+i,nxtchar
    if nxtchar=0 then ret
    if data_in[noparse][[/noparse]i]<>nxtchar then Matchtop
    next
    ' found it!
    Match=(ctest-mystrings)/5+1 ' match is equal to 1, 2, 3, etc.
    Ret:
    return

    Mystrings: data "f","g","1","z","0"
    data "c","m","d","0","2"
    data 0 ' end of list

    Then you could use Branch after this call to jump around.

    Warning: I'm half awake a 0650, so this might be off a bit, but the idea
    should be sound. I'm sure someone will point out my mistakes :-)

    Good luck!

    Al Williams
    AWC
    * 8 channels of PWM
    http://www.al-williams.com/awce/pak5.htm


    >
    Original Message
    > From: debu_sen_22 [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=lNk5RB3KNBA-dK0gOSD__bmSxjGL55bzWwgNHKEb65eq53-cnbmr03YT6OzxdJ6SakcSb8eFJIs6MSjLQ8nm]debu_sen_22@y...[/url
    > Sent: Wednesday, February 27, 2002 8:45 PM
    > To: basicstamps@yahoogroups.com
    > Subject: [noparse][[/noparse]basicstamps] string comparison
    >
    >
    > Hi
    >
    > Is there any way to compare a string with a variable in PBASIC?
    >
    > the following is my code:
    >
    >
    > serin 1, 1021,[noparse][[/noparse]WAIT(".1"), STR data_in\5]
    >
    > if data_in = "fwd1z" then FORWARD
    >
    > if data_in = "rgt1z" then RIGHT
    >
    > if data_in = "lft1z" then LEFT
    >
    > if data_in = "rev1z" then REVERSE
    >
    > if data_in = "stp1z" then STOP
    >
    > if data_in = "sen1z" then SENSOR
    >
    >
    > Now, is there any easier way then having to access each array element
    > and comparing it?
    >
    > Debu
    >
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed. Text in the
    > Subject and Body of the message will be ignored.
    >
    >
    > Your use of Yahoo! Groups is subject to
    > http://docs.yahoo.com/info/terms/
    >
Sign In or Register to comment.