Home AMX User Forum AMX General Discussion

LENGTH_ARRAY // LENGTH_STRING Question

Hi All,

what is the difference between those command, why they didn't reply the same value.
button_Event[dvtp,127]
{
    push:
    {
	local_var char ctemp[5]
	ctemp='Stop:'
	send_string 0,"' LENGTH_ARRAY1: ',itoa(LENGTH_ARRAY(ctemp))"
	send_string 0,"' LENGTH_ARRAY2: ',itoa(LENGTH_ARRAY('Stop:'))"
	send_string 0,"' LENGTH_ARRAY3: ',itoa(LENGTH_ARRAY("'Stop:'"))"
	send_string 0,"' LENGTH_STRING1: ',itoa(LENGTH_STRING(ctemp))"
	send_string 0,"' LENGTH_STRING2: ',itoa(LENGTH_STRING('Stop:'))"
	send_string 0,"' LENGTH_STRING3: ',itoa(LENGTH_STRING("'Stop:'"))"
    }
}

the response i got is:
Line      1 (06:38:28)::   LENGTH_ARRAY1: 5
Line      2 (06:38:28)::   LENGTH_ARRAY2: 1152244836
Line      3 (06:38:28)::   LENGTH_ARRAY3: 1152244836
Line      4 (06:38:28)::   LENGTH_STRING1: 5
Line      5 (06:38:28)::   LENGTH_STRING2: 1152244836
Line      6 (06:38:28)::   LENGTH_STRING3: 1152244836

Line 1 & 4 return the correct answer but why the answer 2,3,5 & 6 are not the same has 1 & 4. It look the same for me. Thanks to help me understand this one.

Comments

  • DHawthorneDHawthorne Posts: 4,584
    LENGTH_ARRAY and LENGTH_STRING return a value that's determined under-the-hood, and seems, to my observations, to be set when you do an operation on the array or string. The don't seem to run through the calculation at the time the function is called. If you directly manipulate an array (I'm including a string array in this), the LENGTH_ function isn't going to reflect your manipulation. But if you use a string operator or variable assignment, it will. My thought in your example is that by assigning your string to a variable, the number gets set. But when you run it on a constant that's essentially assigned on the spot, that calculation hasn't happened and you are getting a meaningless result.

    Perhaps someone knows the rules better, but when I run across something wonky like this, I just backpedal and find the method that gives the correct result and move on. With an interpreted language like Netlinx that has a limited application, sometimes that's the best you can do unless you were involved in writing the compiler.
  • Jorde_VJorde_V Posts: 393
    Raphayo wrote: »
    Hi All,

    what is the difference between those command, why they didn't reply the same value.

    Line 1 & 4 return the correct answer but why the answer 2,3,5 & 6 are not the same has 1 & 4. It look the same for me. Thanks to help me understand this one.
    Line      1 (08:19:03.132)::  LA1 nor:5
    Line      3 (08:19:03.134)::  LA2 max:5
    Line      4 (08:19:03.135)::  LA3 nor:5
    Line      5 (08:19:03.136)::  LA4 max:5
    Line      6 (08:19:03.137)::  LS1 nor:5
    Line      7 (08:19:03.138)::  LS2 max:5
    Line      8 (08:19:03.139)::  LS3 nor:5
    Line      9 (08:19:03.140)::  LS4 max:5
    Line     10 (08:19:03.141)::  LA5 nor:0
    Line     11 (08:19:03.142)::  LA6 max:0
    Line     12 (08:19:03.143)::  LS5 nor:0
    Line     13 (08:19:03.145)::  LS6 max:0
    

    Is the output I get with the following code:
    DEFINE_VARIABLE
    char    sTemp[] = 'Stop:';
    
    DEFINE_START
    WAIT 500{
        local_var char cTemp[5];
        cTemp = 'Stop:';
        
        send_string 0,"'LA1 nor:',itoa(length_array(ctemp))";
        send_string 0,"'LA2 max:',itoa(max_length_array(ctemp))";
        send_string 0,"'LA3 nor:',itoa(length_array(stemp))";
        send_string 0,"'LA4 max:',itoa(max_length_array(stemp))";
        send_string 0,"'LS1 nor:',itoa(length_string(ctemp))";
        send_string 0,"'LS2 max:',itoa(max_length_string(ctemp))";
        send_string 0,"'LS3 nor:',itoa(length_string(stemp))";
        send_string 0,"'LS4 max:',itoa(max_length_string(stemp))";
        send_string 0,"'LA5 nor:',itoa(length_array('Stop:'))";
        send_string 0,"'LA6 max:',itoa(max_length_array('Stop:'))";
        send_string 0,"'LS5 nor:',itoa(length_string('Stop:'))";
        send_string 0,"'LS6 max:',itoa(max_length_string('Stop:'))";
    }
    

    I'm running it on a ni-3100 running 4.1.404.
  • DHawthorneDHawthorne Posts: 4,584
    Bottom line is you can't put a string constant directly as a parameter on those functions and get the result you are expecting. I could make a few guesses why pertaining to how string space is normally allocated by compilers, but that is the issue. You have to assign your string to a variable first, then run the variable as your parameter.
Sign In or Register to comment.