Comparing String Variables on a BS2?
Bottle
Posts: 15
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...
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
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 -->
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...
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·
Regards,
Bruce Bates
Post Edited (Bruce Bates) : 9/8/2006 12:58:13 PM GMT
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...
http://www.parallax.com/detail.asp?product_id=28140
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
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...
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 Savage
Parallax Tech Support