|
Sure: there's no special syntax needed for arrays, or even associative arrays (where the "index" is a string not an integer). You just make the variable name include both the array and the index. E.g.
variable 'myArray_1' write 'first' close
variable 'myArray_2' write 'second' close
The structure of the variable names themselves is up to you: if you prefer $myArray[1] you can build it like that too. You can also use a shorter syntax for assignment, if you have 4.5 SR1 (everything here works in both 4.5 and 4.5 SR1, unless stated otherwise):
$myArray[1]='first'
Using the longer variable...write...close syntax lets you build up the variable name bit by bit, which is useful when you want the index to vary. The following will create $myArray_1 containing the name of the first object of type 'MyObjectType', $myArray_2 for the second:
$ix='1'
foreach .MyObjectType
{
variable
'myArray_' $ix++
write
id
close
}
Perhaps better for many cases is to use one long string variable, with each line being one element of the array. You can iterate over the elements of such variables easily in 4.5 SR1:
$myLines=''
foreach .MyObjectType
{
variable 'myLines' append
if $myLines then newline endif /* prevent an extra newline at the start */
id
close
}
/* Later, to iterate over the "array" (requires 4.5 SR1) */
do $myLines
{
id
}
For an associative variable, e.g. to be able to look up the type later:
foreach .MyObjectType
{
variable 'typeForId_'
id
write
type
close
}
/* Later, if we have a variable $thisId and want to output its type */
variable 'typeForId_' $thisId read
Having said all that, normally there's no need for variables. The information you need is generally to be found on the stack with id;1 etc. That's nicer too, because you don't need to do any work for it, and you have full access to the information of that object. Variables only store string versions of the information, so if you need the id, the :Foo property and the type, you'd need to create variables for all of them. From the stack, you can just pick them up as id;1 :Foo;1 type;1 etc.
|