One of the most interesting and useful variables are arrays and hashes. The are single variables but contain numerous values - with hashes they can even have a relation to each other. Please note that when accessing arrays by number the first element in an array always starts with 0.
To initialize an array write:
@myarray = ("red","green","black"); or
@myarray = qw(red green black); or
$myarray[0] = "red";
$myarray[1] = "green";
$myarray[2] = "black";
To use this array I have a little example once again which could be used in a CGI environment:
1: @colors = qw(green red orange blue black);
2:
3: for (sort @colors)
4: { print "<FONT COLOR=\"$_\">$_</FONT><BR>\n" }
5: print "<BR>There are $#colors colors available";
And this is what we see:
<FONT COLOR="black">black</FONT><BR>
<FONT COLOR="blue">blue</FONT><BR>
<FONT COLOR="green">green</FONT><BR>
<FONT COLOR="orange">orange</FONT><BR>
<FONT COLOR="red">red</FONT><BR>
<BR>There are 5 colors available
Line 6 just iterates through all entries in the @colors array and line 7 creates a print command that produces our desired output. \" is used instead of " since a single quotation mark would end our print command. Note the sort in line 3 which sorts the output alphabetically. If you remove the sort, the output would be in the original order (green, red ...)
*) A preceeding backslash introduces a special character to PERL, like " $
*) The variable $_ holds the current entry in the array which is also true for other loops
*) $#array returns the number of elements in an array