View unanswered posts | View active topics It is currently Fri Apr 17, 2026 11:16 pm



Reply to topic  [ 7 posts ] 
 specifying the end of an array 
Author Message
Ensign

Joined: Wed Nov 06, 2002 3:00 am
Posts: 270
Unread post 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.


Mon Aug 24, 2009 10:40 pm
Profile
Commander

Joined: Sun Feb 25, 2001 3:00 am
Posts: 1838
Location: Guam USA
Unread post 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

_________________
TWGS V2 Vids World on Guam Port 2002
Telnet://vkworld.ddns.net:2002
Discord @ DiverDave#8374
Vid's World Discord

Founding Member -=[Team Kraaken]=- Ka Pla

Image
Winners of Gridwars 2010
MBN Fall Tournament 2011 winners Team Kraaken
Undisputed Champions of 2019 HHT Just for showing up!

The Oldist , Longist Running , Orginal Registered Owner of a TWGS server :
Vids World On Guam


Tue Aug 25, 2009 2:26 am
Profile WWW
Commander
User avatar

Joined: Tue Oct 07, 2003 2:00 am
Posts: 1134
Location: Augusta, GA
Unread post 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.

_________________
Claim to Fame: only guy to ever crack the TW haggle algorithm, and fig/shield/hold price formula, twice.


Tue Aug 25, 2009 10:44 am
Profile WWW
Ensign

Joined: Wed Nov 06, 2002 3:00 am
Posts: 270
Unread post 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.


Wed Aug 26, 2009 9:30 pm
Profile
Commander
User avatar

Joined: Tue Oct 07, 2003 2:00 am
Posts: 1134
Location: Augusta, GA
Unread post 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+

_________________
Claim to Fame: only guy to ever crack the TW haggle algorithm, and fig/shield/hold price formula, twice.


Sat Aug 29, 2009 12:13 am
Profile WWW
Ensign

Joined: Wed Nov 06, 2002 3:00 am
Posts: 270
Unread post 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


Wed Apr 11, 2012 4:24 am
Profile
Gameop
User avatar

Joined: Tue Nov 19, 2002 3:00 am
Posts: 1050
Location: USA
Unread post 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.

_________________
Dark Dominion TWGS
Telnet://twgs.darkworlds.org:23
ICQ#31380757, -=English 101 pwns me=-
"This one claims to have been playing since 1993 and didn't know upgrading a port would raise his alignment."


Wed Apr 18, 2012 9:59 pm
Profile ICQ
Display posts from previous:  Sort by  
Reply to topic   [ 7 posts ] 

Who is online

Users browsing this forum: No registered users and 20 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by wSTSoftware.