www.ClassicTW.com
http://classictw.com/

specifying the end of an array
http://classictw.com/viewtopic.php?f=15&t=22863
Page 1 of 1

Author:  SteveH_66 [ Mon Aug 24, 2009 10:40 pm ]
Post subject:  specifying the end of an array

What is the best way to specify the end of an array when writing a script? I am doing a while loop to step through the array, to check if the array has a match to the other variable. Here is the code I am using.

Code:
WHILE ($visitedPorts[$i]<>0) AND ($visitedPorts[$i]<>$portSector)
add $i 1
end


This is the error message I am getting when the script terminates :
How many holds of Fuel Ore do you want to sell [150]? Script run-time error in '
_EXPLORINGINITIALPORTVALUES.TS': Static array index '0' is out of range (must b
e 1-1), line 27


So I assume that you can't use 0 to specify the end of the array.

Author:  Vid Kid [ Tue Aug 25, 2009 2:26 am ]
Post subject:  Re: specifying the end of an array

I do like this :

GetInput $File "Name of File ? "
FileExists $exists $File
If ($exists)
ReadToArray $File $ports
SetVar $MaxCount $ports
SetVar $s "1"
Else
Echo "* You have No File*"
End

Echo "*"
While ($s <= $MaxCount)
Echo "*"& $ports[$s]
Add $s "1"
End
Echo "*"


Then as you can see you are using the maxcount as your end determinater.

hope this helps

Author:  ElderProphet [ Tue Aug 25, 2009 10:44 am ]
Post subject:  Re: specifying the end of an array

I always use a variable with the same name as the array to store the array size.
So if I want to iterate over the elements in the $array, my code would look like this:
Code:
setVar $i 1
while ($i <= $array)
    echo "*Element " $i " = " $array[$i]
    add $i 1
end

You can see this in the way that I setup the ReadToArray command, which Vid just referenced. It already creates the variable with the same name as the array, so you can use it for your limit (assuming you don't alter it).
Code:
readToArray $file $portsArray
setVar $i 1
while ($i <= $portsArray)
    echo "*" $portsArray[$i]
    add $i 1
end

I even use this same technique on multi-dimensional arrays, so $array[$dim1] would be the number of elements in that dimension, and I would iterate over those elements similarly:
Code:
setVar $i 1
while ($i <= $array[$dim1])
    echo "*" $array[$dim1][$i]
    add $i 1
end
I have some better examples on multi-dimension arrays too, which is really where this naming technique shines. You don't have to keep track of myriad sizes, they are built in (sorta). Let me know if you are interested and I'll share them.

Author:  SteveH_66 [ Wed Aug 26, 2009 9:30 pm ]
Post subject:  Re: specifying the end of an array

Thanks Vid and +EP+ that was a lot of help. +EP+ I would be interested to learn more even though I haven't tried multidimensional arrays yet, any opportunity for further learning is much appreciated.

Author:  ElderProphet [ Sat Aug 29, 2009 12:13 am ]
Post subject:  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+

Author:  SteveH_66 [ Wed Apr 11, 2012 4:24 am ]
Post subject:  Re: specifying the end of an array

ElderProphet wrote:
I have some better examples on multi-dimension arrays too, which is really where this naming technique shines. You don't have to keep track of myriad sizes, they are built in (sorta). Let me know if you are interested and I'll share them.


Hey EP, when we were doing this discussion a couple of years ago I forgot to tell you I would be very interested in those other examples on multi-dimension arrays. If you get some spare time, I would be very interested in them if you would care to post them. Thanks

Author:  Kaus [ Wed Apr 18, 2012 9:59 pm ]
Post subject:  Re: specifying the end of an array

ElderProphet wrote:
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+


Good stuff, I second an additional example request.

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/