IPTSCRAE SCRIPTING LESSON SEVEN
Array of Hope
Just what IS an array, you ask? Well, it is an ordered list of other Iptscrae data types. Arrays must be encased in squarebrackets ( [ and ] ). Arrays can be composed of different data types, including other arrays. Examples:

[ 100 200 300 ] [ "Hello" "Goodbye" ] [ 100 "blue" [ 1 2 3 ] ]

The first example is an array of numbers. It contains 3 items of data, separated by spaces. The second example is an array of strings that contains 2 elements. The third example is an array that contains 3 elements, a number, a string, and another array!

You can assign arrays to a variable, just as you do with any other data types. For example:

[ "dog" "cat" "pig" "horse" ] animals =

assigns the array to the variable called "animals". The only thing you can't do with an array, that you do with other data types, is declare it GLOBAL. (Although, there IS a way to get around that difficulty, as you will see later!)

The maximum number of elements than can be assigned to an array is 32, although you can get around this by storing arrays within arrays.

There would be no reason to bother with an array, unless there were methods for storing and retrieving it's data. The following functions do just that:

<array> <index> GET
This function gets item number index from array. Note that the elements of an array are numbered from 0 (zero) to (number of elements minus 1). index is the number of the element of the array that you want to retrieve. The array may be specified directly (element by element) or by reference to its variable name. For example:

[ "dog" "cat" "pig" "horse" ] 3 GET SAY

is a method used to say the fourth element of the array (remember, the FIRST element is element 0 (zero), not 1!) which in this case, is "horse". So when this line of code is run, the user would say "horse".

<data> <array> <index> PUT
This command is used to put data into an array, in the position indicated by index. If the data is a string (as opposed to an integer), it must be encased in doublequotes. Note that the elements of an array are numbered from 0 (zero) to (number of elements minus 1). For example:

[ "dog" "cat" "pig" "horse" ] pets = "parakeet" pets 2 PUT

would put the string "parakeet" in the array named pets, completely replacing the word "pig". Remember: we start counting with 0! Although you CAN use:

"parakeet" [ "dog" "cat" "pig" "horse" ] 2 PUT

there is not much point in trying PUT data into an array that you have NOT assigned to a variable. When the command is completed, it throws the array back off the stack, so all your work would be lost.

If you need to see how many elements there are in an array, you may use:

<array> LENGTH
This function returns the number of elements in array. The array may be specified directly (element by element) or by reference to its variable name. Remember, that when you are addressing each element, it is:

array LENGTH 1 -

because you start counting at 0.

A Magical Tool

{ atomList } [ array ] FOREACH
This command executes atomList once for each item in array. Before executing the atomlist, each item in the array is pushed onto the stack. The atomlist should be something that pops these items off the stack and does something with them. For example:

{ item =
  { item 1 GET SAY } CHATSTR LOWERCASE item 0 GET SUBSTR IF }
  [ [ "horse" "!NEIGH!" ]
  [ "pig" "Oink, oink!" ]
  [ "cat" "!MEEOWW!" ]
  [ "dog" "Woof, woof!" ]
  [ "cow" "mmoooooo" ]
] FOREACH

The FOREACH command would load each element in the original array onto the stack, then run the routine on it. Because this is an "array of arrays", the elements put on the stack are also arrays, consisting of 2 elements, the name of an animal, and the sound that animal makes. The array from the stack is assigned to "item", and the atomlist inside the routine checks to see if the CHATSTR contains the first item in the array. If it does, it runs the routine, which SAYs the second item in the array. In other words, you respond with the appropriate animal sound whenever someone mentions that animal.

Don't go there!
The documentation for Iptscrae mentions one other command specifically for arrays. It is:

<number> ARRAY
According to the documentation, this command creates an array containing number elements. This array will contain no data when first created; it will consist of a number of empty "slots" awaiting input via the PUT command. However, both Foxy and Tandika have experimented with this and have never gotten it to work. This is because it does not work for the PC client, although it does for the MAC. If, for some reason you need to make an "empty" array to use later on, you can do it like this:

[ 0 0 0 0 0 0 ] variable =

putting the appropriate number of 0's (zeros) in the array as placeholders.

HOMEWORK:
1. Make a routine which will load all of your prop id's of your current AV into an array, strip you naked prop by prop, move you to the mouse position, then put the pieces back on in reverse order by taking the numbers from the array. Have it go off when you say "port".

2. Make a routine that stores information about the 6 color sets that you used to make your rainbow in lesson 3 in an array. Have it go off when someone asks you, "What are the numbers for <colorName>?" As an example, if someone were to say "What are the numbers for red?", your response would be "The numbers for red are 255 0 0."

CHALLENGE:
After reading through the notes below, write a routine that uses STRTOATOM in a way different from that used as the example. NOTES Trick or Treat!:
You may recall that variables that hold arrays cannot be declared GLOBAL Well, here is a "trick" to get around that. You make the array a string, and then use the following command:

<string> STRTOATOM EXEC
This command turns a character string into an executable atomList.

For example:

aryStr GLOBAL "[ 1 2 3 ]" aryStr =
{ ITOA SAY } aryStr STRTOATOM EXEC FOREACH

What this does is changes the string, stored in the variable "aryStr" back into an array for the FOREACH command to manipulate. This is a very handy way to store arrays for later manipulation.

And STRTOATOM isn't reserved for just this use. It can change ANY string (provided it contains valid code) into an executable piece of code! With a little imagination you can do some pretty tricky things with this one.

<Previous> <Iptscrae> <Next>