Perl Basics - Part 2

Now let's get to some slightly more advanced stuff.
#!/usr/local/bin/perl                                                  # 1
@friends=("Pat","Chris","Jean");                                       # 2
$counter=0;while ($counter <= 2)                                       # 3
{print "My favorite friend today is $friends[$counter]\n";$counter++;} # 4

The first thing you'll probably notice is the @. @friends is an array. It is actually a collection of values held under one name. The easiest way to refer to these values individually is by number. Line 2 could also have been broken up into three statements like this:

$friends[0]="Pat";
$friends[1]="Chris";
$friends[2]="Jean";

Notice how, true to binary ways, the first element of the array is at number 0. Also notice that when refering to a single element of an array, the array name is preceeded by a $ rather than the @ (it's $friends[0] not @friends[0])

We can put the power of arrays to use by using repeat loops. Using a while command with the $counter variable (line 3), we can do calculations on the array elements one at a time.

In line four, we are simply printing a line which includes a particular element of the array. For example, on the first time through the loop, $counter is 0:

$friends[$counter] ---> $friends[0] ---> "Pat"
Notice the \n. It's a newline character (like a carriage return) which keeps the lines from running together. They must be in quotes, and Perl doesn't put them in for you.

After we print out one line, we add one to $counter ($counter++;) and go through the loop again with the next element of @friends.



And some more:

#!/usr/local/bin/perl                           #1
%title=("Bill","Mr. ",
        "Manners","Miss ",
        "Kirk","Captain ");
@people=("Kirk","Bill","Manners");              #5
foreach $person (@people)
  {print $title{$person}.$person."rocks!";}

Ok, we've got 3 new things here:

%title=...
This is an associative array. It consists of keys and values You can refer to the value by the name of the associative array and the key. Here, we defined an array with keys Bill, Manners, and Kirk with values of Mr. , Miss. , and Captain respectively. The way to get the value from a key is like this: $array{'key'}='value'; here: $title{'Bill'}='Mr. '; $title{'Kirk'}='Captain '; and $title{'Manners'}='Miss';. Notice that you refer to an element of a regular array with the array title followed by a number between square brackets whereas with associative arrays, you refer to elements with the array title followed by a string in curly brackets.

Note that although I put each key/value pair on a seperate line, since Perl doesn't care about whitespace, they can be right next to each other. Just remember that it goes (key, value, key, value,...).

foreach $person (@people)
This is another good way to count through arrays. What this line translates to is "loop through the following block (lines of code surrounded by curly brackets, like those after conditional or loop statements, are called blocks) once for each element of the array in parentheses, setting the variable named in between to the current element of the array". Here, first the loop is executed with $person="Kirk". After that iteration has finished, it runs through with $person="Bill". And so on until all the values of @people have been exhausted.
$title{$person}.$person."rocks!";
It's the periods which are the mildly interesting thing here. They simply concatenate two strings. They're only really necessary if otherwise Perl couldn't tell where a variable name ends and text to be printed out begins. For example here, if we had print "$title{$person}$personrocks!";, Perl would assume we were talking about a variable called $personrocks.



Ok, if you're still with me up to here, you're ready for the next step which is actual CGI stuff.

If not, you might want to back up and go a little slower.