Array Issue
colinhasted
Posts: 44
Hi All,
This one could have a simple answer, I just can't see the wood for the trees.
Is there a neater way of clearing an array? I am working with a Long array
LONG l[50]
I have a process which repeatedly fills the array with various lengths which leaves the garbage from the existing run at the end of the array.
How does one clear the array in one hit? Chars are fine, "" some quotes do the job. However I can't seem to find a way of 'nulling' the variable without running a loop.
Surely there must be an easier way?
This one could have a simple answer, I just can't see the wood for the trees.
Is there a neater way of clearing an array? I am working with a Long array
LONG l[50]
I have a process which repeatedly fills the array with various lengths which leaves the garbage from the existing run at the end of the array.
How does one clear the array in one hit? Chars are fine, "" some quotes do the job. However I can't seem to find a way of 'nulling' the variable without running a loop.
Surely there must be an easier way?
0
Comments
set_length_array
look up this key word. set length to zero
DEFINE_START
ArrayNull = "0,0,0,0,0,0,0,0,0,0"
Then in my code, I set my array to ArrayNull:
WorkingArray = ArrayNull
And I clear out any old values in each individual slot. I can then also set the length to zero, but I know that there are no left over values.
If your actual values are valid from -MinValue to MaxValue, that's a bit tougher, but I suspect that there is a specific range of values that you consider valid. Set your null value to anything outside that range.
I have two arrays one declared in define variable and a stack_var which is used as a temporary store whilst shuffling data around. This is then copied back and the iterating starts over until the result can get no smaller.
It appears there are two issues, one is to clear out the arrays on each run of the function to make sure I don't end up with any garbage. The other is it appears when creating a stack_var long it picks up whatever garbage is left in the memory it has been assigned. I may be wrong with this but it appears that integer stack_vars don't have this issue, they always seem to declare empty.
Programatically I guess there's not much in it between svTECH's null file idea and quickly running a nulling loop (as I already have a counter declared for the next operation). Being lazy though, the for loop means I don't have to create a large variable full of zeros at the beginning!
Thanks all for your help!
Make the null value array something like 10 chars long and then use a for loop to go through the larger buffer 10 chars at a time. That might cut things down a bit. But maybe not.
Have you thought about just "nulling" (making zero) the index just after your final? For example:
1,2,3,4,6,4,3,0,21,5,2
This way you know where it "ends". If you know where the end is supposed to be, why does it matter if there's more after it?
0 values are valid so a zero would not necessarily mean the end of a string.
A counter and a null of the array before use has got me ticking over nicely!
eric - good thought on the nulling!
I tried a quick test where I created a STACK_VAR array of LONG and a STACK_VAR array of INTEGER then printed these out to debug in ascii using FORMAT.
All manner of numbers surfaced from both of them!
Shows the importance of making sure I clear my variables first. I guess I've never noticed it before as usually I copy something into that variable before doing anything with it thus wiping all the garbage.
This directly contradicts the NetLinx Keywords Help File from Studio:
Hmm, will have to run some tests and see if I can replicate.
I would run tests but I have my laborer's hat on this month not my programer's hat so i'm avoiding the office.