Home AMX User Forum NetLinx Studio

Populating a CHAR variable slot by slot

CHAR cARRAY1[500]
CHAR cARRAY2[100]

FOR (blah;blah;blah)
{
IF (whatever)
cARRAY1[x]=cARRAY2[y]
}

I have some data in cARRAY1 Which is being looked over and I want to take certain bytes of it and copy them selectively into cARRAY2. Is this not possible to populate such an array randomly?

Thank you.

Comments

  • Joe HebertJoe Hebert Posts: 2,159
    Sure it?s possible. I don?t know what the rest of your code looks like or what?s not working for you but I?m going to venture a guess that you aren?t seeing the correct results for cARRAY1. If you haven?t set the length to cARRAY1 before your blah blah blah and whatever, the length is not going to change by assigning one character to a position in the array.

    Try putting these two lines under DEFINE_START and see if it does the trick.

    SET_LENGTH_ARRAY(cARRAY1,500)
    SET_LENGTH_ARRAY(cARRAY2,100)

    If not you?ll have to give us a little more information on what?s not working.

    Joe
  • frthomasfrthomas Posts: 176
    Or do

    if (whatever){
    cARRAY1[x]=cARRAY2[y];
    SET_LENGTH_ARRAY(cArray1, LENGTH_ARRAY(cARRAY1)+1);
    }

    Not efficient maybe, but conceptually right.

    Fred
  • TurnipTruckTurnipTruck Posts: 1,485
    SET_LENGTH_ARRAY did the trick! Thank you!

    How can I clear the contents while keeping the length at what I set it for?

    cARRAY='' sets the length to zero.

    Thank you!
  • Joe HebertJoe Hebert Posts: 2,159
    Correct: cArray1=?? will set the length to zero but that?s all it will do. It does not clear the contents of memory. If you want to clear out the contents you will need to loop through the array and clear each pos one at a time.

    Joe
  • DHawthorneDHawthorne Posts: 4,584
    As an aside, CLEAR_BUFFER also sets the length to zero, but does not actually clear the data. If it's a small array, I just do something like:

    sArray = "0,0,0,0,0,0"

    If that is not feasible due to size, I'll run it throught a FOR loop:

    INTEGER iCount ;

    FOR(iCount = 1; iCount <= LENGTH_ARRAY(sArray); iCount ++) {
    sArray[iCount] = 0 ; }

    This assumes, of course, you have previously used SET_LENGTH_ARRAY to make sure the entire array is dealt with; that, or use a constant value instead of LENGTH_ARRAY equal to the declared dimension of the array.
  • frthomasfrthomas Posts: 176
    How can I clear the contents while keeping the length at what I set it for?

    Well, you can't. Technically speaking there is always a content, even if it is 0x00 (i.e. you have a string of 0x00). In this respect the Netlinx implementation is a bit half finished, since as you noted you can access individual chars outside of the string length (only the max_length is checked, which exposes the underlying implementation).

    From a memory perspective, your arrays will always occupy the space needed for their MAX_LENGTH. This is constant.

    I'd vote for always assigning the array length. If you clear it, then set the length to 0. Use the length as the indication of the content, not the individual char content. I mean why would you want to clear the content without changing the length, the only reason I can think is that you want to test the content to detect what's in there. And the length is designed for that.

    We could certainly benchmark this but setting the array length is not more expensive that assigning a variable. And assigning the length to 0 is certainly faster than walking the array to set all chars to 0.

    My two cents

    Fred
Sign In or Register to comment.