Shop OBEX P1 Docs P2 Docs Learn Events
Comparing String Variables on a BS2? — Parallax Forums

Comparing String Variables on a BS2?

BottleBottle Posts: 15
edited 2006-09-08 17:33 in BASIC Stamp
Howdy

Have had some recent success using the BS2 and a PINK module, so that I can enter a string on the PINK, and have the BS2 extract that information.

All well and good

What I'm trying to do now is to write some code to compare the entered strings against a pre-programmed list, so that entering a string results in a particular action happening, thus for example:

IF input_string = string_A THEN GOSUB A:
IF input_string = string_B THEN GOSUB B:

etc

I am having difficulty in getting the syntax right. Would having a CASE structure be more beneficial?

I'm just banging my head against a wall at the moment, have had a look through the Basic Stamp manual, but I could't find anything close to what I'm attempting to achieve.

Hope you guys (and girls) can help

Ta, Bottle

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
B.·· ;-)


There are 10 types of people in the world - those who can count in binary, and those who can't...

Comments

  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-09-08 11:25
    Bottle -

    There are no string handling facilities in PBASIC, so comparison would have to be made on a character by character basis. This is eased somewhat by knowing the following. Any PBASIC variable can be treated as an array (subscripted) whether or not it's defined as an array!

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    <!--StartFragment -->
  • BottleBottle Posts: 15
    edited 2006-09-08 11:41
    Bruce

    Hello again!

    OK, that that sorts makes sense then (it's not just me!).


    Does it matter if the strings are not all the same length?

    Would I have to define an array that handles the maximum length of variable I'm expecting?

    Anyway, thanks for the pointers - I'll read up on Arrays during my lunch-break

    BTW, thanks for the help previously with the PINK module - got it to work reliably with the BS2 and the LCD display

    Thx

    Bottle

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    B.·· ;-)


    There are 10 types of people in the world - those who can count in binary, and those who can't...
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-09-08 12:29
    Bottle -

    You asked: "Does it matter if the strings are not all the same length?" which becomes a general case question, since we can not be confined to the PBASIC Syntax or rules - it has no string handling, as earlier noted. In that respect, different languages or language varients will handle that situation differently, so you will have to make·your rules for variable length strings, as you see fit. Here are some of the ways that situation is handled by various other languages and varients:

    Ex. 1:

    In the case of unequal length strings, IF the shorter string can be padded out with [noparse][[/noparse]some character, usually blanks] and the strings are then equal, they are then considered equal to begin with.

    Ex. 2:

    In the case of unequal length strings, regardless of the excess content of the longer string, the smaller stirng is always deemed to be LESS than the longer string.

    Ex. 3:

    In the case of unequal length strings, the longer string is truncated to the length of the smaller string, and the comparison then proceeds as if they were equal length strings.

    =-=-=-=-=-=

    Personally, I more tend to agree with Ex. 1, with either blanks or nulls as the substitute character, depending on the situation, and presuming you'd like to have an opinion.

    You also asked: "Would I have to define an array that handles the maximum length of variable I'm expecting?"

    The answer to that is YES, since there is no support for dynamic array allocation in PBASIC, which would be the only alternative of which I'm aware.

    You're quite welcome for any previous help. I wouldn't be doing this if I didn't enjoy it immensely, or if it wasn't recommended by my psychiatrist·smile.gif

    Regards,

    Bruce Bates

    Post Edited (Bruce Bates) : 9/8/2006 12:58:13 PM GMT
  • BottleBottle Posts: 15
    edited 2006-09-08 13:27
    Bruce

    Thanks for your insight, which is extremely valuable.

    I'm revising the whole setup at the moment, and looking at other alternatives. I'll let you know how it pans out.

    Should have some stuff uploaded later - stay tuned!

    Regards

    Bottle

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    B.·· ;-)


    There are 10 types of people in the world - those who can count in binary, and those who can't...
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-09-08 15:45
    Coming into this a bit late, but if you check the example source code on the following page you can see an example of comparing two strings to determine a match.· I hope this helps.· Take care.

    http://www.parallax.com/detail.asp?product_id=28140

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • BottleBottle Posts: 15
    edited 2006-09-08 15:53
    Chris

    Aha! I see the glimmer of hope there.

    Just a quick question, if I was to remove the code for the SPRAM (as I'm running a BS2IC), can I still read & write data to the EEPROM?

    I guess I'd need to write in a table of all the strings I wish to compare, and then take it from there.

    Anyway, I've got my thinking hat on, and I'll let you know how it goes

    Ta Muchly

    Bottle

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    B.·· ;-)


    There are 10 types of people in the world - those who can count in binary, and those who can't...
  • Mike GreenMike Green Posts: 23,101
    edited 2006-09-08 16:04
    You can read and write data to the EEPROM, but remember that you can only write to a given location maybe 100000 times before it "wears out". It's really easy to get to that many times programmatically. Often, if the data changes slowly, using the EEPROM is still reasonable. What's important is checking first to see if the stored data value is the same as the one you're planning on writing, then skipping the write like:
      READ location,value
      IF value = newValue THEN skipIt
      WRITE location,newValue
    skipIt:
    
    
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-09-08 16:10
    Bottle -

    Yes, you can use EEPROM, but there is a caveat here. EEPROM has a finite lifespan, and once you reach it (10,000-100,000 writes depending on the chip), you're faced with replacing it, so it would be wise to use the Stamp's EEPROM frugally.

    An external EEPROM (or other type of external memory) is reasonably easy to implement, and if it ever needs to be replaced, it's external to the PBASIC Stamp facility. If it's socketed, you're talking a 2-3 minute process at most!

    Additionally, there are other external memory solutions with significantly longer lifespans; some nearly infinite as compared to the expected life of your product or project.

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    <!--StartFragment -->
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-09-08 17:33
    Bottle said...(trimmed)
    Aha! I see the glimmer of hope there.
    Just a quick question, if I was to remove the code for the SPRAM (as I'm running a BS2IC), can I still read & write data to the EEPROM?
    I guess I'd need to write in a table of all the strings I wish to compare, and then take it from there.
    There is no need to write to EEPROM...If you'll notice the code is designed to work with any BASIC Stamp.· It uses an array on the BS2 to compare the string against that stored in EEPROM.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
Sign In or Register to comment.