Populating a CHAR variable slot by slot
TurnipTruck
Posts: 1,485
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.
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.
0
Comments
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
if (whatever){
cARRAY1[x]=cARRAY2[y];
SET_LENGTH_ARRAY(cArray1, LENGTH_ARRAY(cARRAY1)+1);
}
Not efficient maybe, but conceptually right.
Fred
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
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.
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