Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Due to spam on this forum, all posts now need moderator approval.
Entire forum
➜ MUSHclient
➜ Perlscript
➜ arrays
It is now over 60 days since the last post. This thread is closed.
Refresh page
Posted by
| Girdy
(9 posts) Bio
|
Date
| Tue 14 Jan 2003 12:58 AM (UTC) |
Message
| 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. | Top |
|
Posted by
| Nick Gammon
Australia (23,133 posts) Bio
Forum Administrator |
Date
| Reply #1 on Tue 14 Jan 2003 09:32 PM (UTC) |
Message
| 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:
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:
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. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Dubthach
(47 posts) Bio
|
Date
| Reply #2 on Thu 16 Jan 2003 02:25 PM (UTC) |
Message
| 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);
| Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
12,872 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top