www.ClassicTW.com
https://mail.black-squirrel.com/

Scripting: Arrays
https://mail.black-squirrel.com/viewtopic.php?f=15&t=21228
Page 1 of 1

Author:  Promethius [ Wed Nov 26, 2008 9:51 pm ]
Post subject:  Scripting: Arrays

Arrays can be thought of as containers for common items. Basically an easier
way of doing some things than with variables.

With normal variables you would setup something like:
setVar $sector1 1
setVar $sector2 2
setVar $sector3 3
...
setVar $sector20000 20000

Kind of time consuming and referencing them would be a nightmare - besides I
really don't want carpal-tunnel.

With arrays you could setup a loop that would be something like:

setVar $i 1 // init the var $i to equal 1
while ($i <= SECTORS) // while value of $i <= value of SECTORS (SECTORS number of sectors in TWX database)
setVar $sector[$i] $i // set $sector[$i] to value of $i ($sector[1] = 1)
add $i 1 // increment our counter so no endless loop
end

The above example shows dynamic arrays. Dynamic arrays are slower than static
arrays from what I understand, but with dynamic arrays you don't have to worry
as much about going beyond the boundary (working with an array that is set to say
5000, and you want to go to 5001).

Static arrays are initialized to a set size and the example above would be:

setArray $sector SECTORS // Each $sector[Number] has a value of 0 until changed
// $sector[1] = 0, $sector[2]= 0, ... $sector[20000] = 0.
setVar $i 1
while ($i <= SECTORS)
setVar $sector[$i] $i
add $i 1
end

No difference in the code except for initing the array.

k, the above examples don't do anything practical so....

# Get a list of deployed figs

setVar $myCount 0 // Counter for number of sectors with figs
setVar $figsFile GAMENAME & ".figs" // setup our file to write to (GAMENAME stored in TWX)
send "'Updating fighter list*" // Send SS so our corpies know we are "away"

delete $figsFile // Delete the old file if it exists
setarray $fighter SECTORS // Set our static array
send "cn1qqg" // send cn1 command to go to pure text, no ANSI
as it seems faster quits the computer and lists figs
waitfor "===" // Waitfor waits on the text shown.

:buildFigArray // label we will use to return to
setTextTrigger Corpfig :addtoList "Cor" // trigger for corp figs
setTextTrigger PersFig :addtoList "Per" // trigger for personal figs
setTextTrigger ListDone :buildFigSec "Command" // trigger for when we are done
pause // pause - waits for trigger activation

:addtoList
KillAllTriggers // killalltriggers - better to kill seperate
getword currentline $figSector 1 // get the 1st word of the line we got from the trigger
setvar $fighter[$figsector] $figsector // set our array to something like $fighter[11] to a value of 11
add $myCount 1 // add $myCount for our total sectors with figs
goto :buildFigArray // go back to check for additional figged sectors

:buildFigSec // label we go to after the fighter list runs
setVar $figSecCounter 11 // $figSecCounter set to 11 - can't have figs in 1-10
while ($figSecCounter <= SECTORS) // while our counter is less than sectors
if ($fighter[$figSecCounter] > 0) // if the value in our array is > 0
setSectorParameter $figSecCounter "FigSec" TRUE // we have a fig, set the setSectorParameter to true
else // else
setSectorParameter $figSecCounter "FigSec" FALSE // we don't have a fig set the parameter to false
end // end the if check
add $figSecCounter 1 // add 1 to our counter
end // end the while

...... // script continues on

The reason for my creating the array of fighters is that it is used later.

Author:  NjoY [ Sat Jan 03, 2009 1:17 pm ]
Post subject:  Re: Scripting: Arrays

Very interesting many thanx.
Tried using arrays a few times and i just get totally lost!!
I'll read that a few times and have a play with them.
Cheers Prome.

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