Comparing strings / char arrays?
fogled@mizzou
Posts: 549
I can't seem to get a char array comparison working. All I'm trying to do is read in some text from 2 different keypads designed for different uses. The first keypad is set to return a string of "KEYP1-xxx" and the 2nd returns "KEYP2-xxx". I can see the data coming into the controller, I'm manipulating it, I can isolate just the KEYP1- or KEYP2- part, but I can't seem to act on it.
I've tried:
... even though I can see that this_array contains KEYP1-. What the heck am I doing wrong?
I've tried:
if(this_array=="'KEYP1-'") // DOES NOT WORK if(this_array=='KEYP1-') // DOES NOT WORK if(this_array==contantKEYP1) // DOES NOT WORK (constantKEYP1 is a char = KEYP1-) if(compare_string(this_array,"'KEYP1-'")) // DOES NOT WORK if(compare_string(this_array,'KEYP1-')) // DOES NOT WORK if(compare_string(this_array,contantKEYP1)) // DOES NOT WORK
... even though I can see that this_array contains KEYP1-. What the heck am I doing wrong?
0
Comments
IF(FIND_STRING(this_array,'KEYP1-',1))
(1) Post the code you use to extract the string
(2) Check for "l" instead of "1" in the string
(3) Try running the code with this line early on and see if it works
this_array = 'KEYP1-'
(4) Write a minimal expression of the fault - that is, a very short program that shows the fault happening - and post it. I would bet that you find the bug in the process.
(5) Add some debug to your code so that you can see exactly what you have in the string just in case you have some whitespace or non-printing characters or the array length is somehow confused:
send_string 0,Hexify(this_array)
1) start a switch-case on a REMOVE_STRING(DATA.TEXT,'=',1)
* in your case the = would be a -, it's important to know what delimits your strings
2) put all the string types you are looking for in the cases
3) do operations with DATA.TEXT.
example: Give that a try.
I was making 2 mistakes yesterday: one was a bug in my debug code (oops!) which kept me from seeing the debug correctly - I think the matches were actually working OK; the other is the single-vs-double quote stuff. In DEFINE_CONSTANT, doing a char variablename[] '"whatever'" actually makes the variable name be 'whatever' (with the single quotes). I was sort of expecting the same behavior in a constant definition as what works in the comparison.
i.e. doing if(variablename=="'TEXT'") will match if the variable content is TEXT, but not if it's 'TEXT'. But... apparently setting a constant to "'TEXT'" makes the variable content 'TEXT', not just TEXT. Oh well.
Yep, it looks like double quote behaves like an alternative to a single quote in a constant declaration but not in a regular assignment.
Text after // shows the result of the assignment in the form it appears on the screen, quotes and all.
[code]
define_constant
char a[100] = "hello" // hello
char a[100] = "'hello'" // 'hello'
char a[100] = "'hello',38" // 'hello',38
char a[100] = '"hello",38' // "hello",38
define_variable
char b[100] = "'hello'" // Compile error RHS not a constant
char b[100] = "'hello',38" // Compile error RHS not a constant
define_program
c = "'hello',38" // hello&