arrays

Posted by Girdy on Tue 14 Jan 2003 12:58 AM — 3 posts, 16,265 views.

#0
Well, things have been going along alright, but I'd like to try to improve the layout a bit, which brings me to the following question.

How do I create and access an array in perlscript? If it's not automatically saved, I'm guessing that I'll have to define it on the world startup or else the script will rewrite the thing everytime.

Some help here would be fantastic.
Australia Forum Administrator #1
I got this stuff by searching for "perlscript array" in Google ...



Perl and PerlScript's primary variable is the scalar datatype. It is declared and initialized as:



  $Name=Value;


A scalar works the same way as a variant—it can store numerals as well as strings, and you never declare one as a specific datatype. Although it's not required, it's good practice to declare the range of the variable upon creation using either the keyword my or local, like this:



 my $data=2001;


Variables of the type my have a scope limited to the block of code in which they're declared, while local variables can be used both within the declaring block and inside any blocks of code called from within that block. (Blocks of code are considered any code within a while loop or subroutine.)
A second datatype is the array, which is a list of elements containing scalar data. An array is declared and initialized in one of these three ways:



 my @name[index]=value;
 my @name=(value, value, value);
 my @name=qw(value value value value);


You must specify my; omitting it causes ASP to set the default scripting language to the name you used for your array because of the similar @ syntax. PerlScript arrays are indexed by integer value, and always have a starting index of zero. To get the total number of indexed elements in an array, PerlScript supports a special variable called $#nameofarray. There are different ways to display one or more variables of an array; the following statements are valid for single elements:



 $Response->Write($name[integer]);
 $Response->Write($name[$variable]);
 $Response->Write($name[$#name]);


To display the very first element, you can set the index to $# or zero; an entire array can be referred to with an @ prefix. These techniques come in handy when you want to loop through an array and print all its elements:



 # Example 1: Looping an array using the special variable $_
 
 for(@name) {
 $Response->Write($_);
 }
 
 
 # Example 2: Looping an array using the special variable $#
 
 for($a=0; $a<=$#name; $a++) {
 $Response->Write($name[$a]);
 }





As for saving arrays, no variable is automatically saved in scripts. You need to do that yourself using MUSHclient variables (eg. world.SetVariable, world.GetVariable). Such variables are always strings. In plugins you can have variables automatically saved to the "plugin state file" otherwise variables are saved when you save the MUSHclient "world" file.
#2
If you want to save an array, but need to use a Mushclient variable...you might want to save it as a delimited string.

To create the string from your array use this syntax:


$delimstring = join(',', @mylist);


And then extracting it from the string once you need to use the array again:


@myarray = split(/,/, $delimstring);