
Re: specifying the end of an array
You can see an example of using a variable with the same name as the array in my post in your other thread,
here.Anyway, the important thing to grasp is that there is an unused element in a TWX array, let me call it the Zero element. If you create an array like this, "setArray $myArray 3", then use $st to echo the script variables, here is what you see:
"$MYARRAY" = "0"
Static array of "$MYARRAY" (size 3)
"1" = "0"
"2" = "0"
"3" = "0"
Look at that first line: "$MYARRAY" = "0". That variable was initialized by TWX
if it didn't already exist. I like to think of this as $myArray[0], hence the term Zero element. This is the variable that I make use of to track the size of the array, especially dynamic arrays. I think its perfect for the job of tracking the size of the array. It's all pretty straight-forward when dealing with a simple one-dimension dynamic array: Every time I add another element, I increment the variable with the same name as the array. For instance, if I wanted to enumerate a list of xxB sectors, I would do it like so:
Code:
setVar $xxB 0
setArray $xxB 0
setVar $a 1
while ($a <= SECTORS)
if (PORT.EXISTS[$a]) and (PORT.BUYEQUIP[$a])
add $xxB 1
setVar $xxB[$xxB] $a
end
add $a 1
end
The line "setVar $xxB[$xxB] $a" can be a bit confusing at a glance, but it's clearer once you understand that $xxB is just being used as a variable.
I hope this is helpful. I feel like arrays that are written in this manner are cleaner, and less prone to programmer error, at least once you are comfortable with $xxB[$xxB], or even scarier, the 2-dimensional variant: $xxB[$a][$xxB[$a]].
+EP+