Home AMX User Forum NetLinx Studio

Comparing strings / char arrays?

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:
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?

Comments

  • Joe HebertJoe Hebert Posts: 2,159
    Try:
    IF(FIND_STRING(this_array,'KEYP1-',1))
  • Here are some ideas to try:

    (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)
    (******************************************************************************)                       
    (* Returns two-character hex representation                                   *)
    define_function char[2] HexifyByte     ( (* Output - Hex String               *)
      integer nArgInput                    ) (* Input  - Number                   *)
    (******************************************************************************)                       
    {
    return right_string("'00',itohex(nArgInput)",2);
    } (* HexifyByte *)
    
    (******************************************************************************)                       
    (* Given any string returns ASCII with embedded Hex values eg [Hello$AB]      *)
    define_function char[255] Hexify       ( (* Output - Ascii/Hex String         *)
      char sArgInput[]                     ) (* Input  - String                   *)
    (******************************************************************************)                       
    {                                                                      
    stack_var char    sMyOutput[1023]
    stack_var integer nMyTemp
    stack_var char    cMyChar                 
                      
    sMyOutput                                        = '['
    
    for (nMyTemp = 1; nMyTemp <= length_string(sArgInput); nMyTemp++)
      {
      cMyChar                                        = sArgInput[nMyTemp]
        
      if (    (cMyChar >= 32)
          and (cMyChar < 126))
        {
        (* Add a good ASCII character in plaintext *)
        sMyOutput                                    = "sMyOutput,cMyChar"
        }
      else if (cMyChar = cLF)
        {
        sMyOutput                                    = "sMyOutput,'<LF>'"
        }
      else if (cMyChar = cCR)
        {
        sMyOutput                                    = "sMyOutput,'<CR>'"
        }
      else
        {
        sMyOutput                                    = "sMyOutput,'$',HexifyByte(cMyChar)"
        }
      } (* for nTemp *)
                                  
    return "sMyOutput,']'";
    } (* Hexify *)     
    
  • DHawthorneDHawthorne Posts: 4,584
    If I'm understanding this correctly, you are working with strings coming back from a panel. If that's the case, those strings are almost never going to be "KEYP1-", because the actual text will be appended to that. You can't test for equivalence unless the entire string is an exact match, you have to use FIND_STRING.
  • mpullinmpullin Posts: 949
    My bread and butter for parsing strings coming back from a NL device is this:

    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:
    DATA_EVENT[dvTP]{
         string:{
              SWITCH(REMOVE_STRING(DATA.TEXT,'-',1)){
                   case 'KEYP1-':
                        SEND_STRING 0, "'This came from keypad 1: ',DATA.TEXT"
                   break
                   case 'PAGE-':
                        SEND_STRING 0, "'The panel just went to page: ',DATA.TEXT"
                   break
              }
         }
    }
    
    Give that a try.
  • Well, all the different ways to match the string (==, compare_string, find_string against "'KEYP1-'", 'KEYP1-' and my kp_identity variable) actually work OK.

    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.
  • Verrrry interesting

    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&
Sign In or Register to comment.