Internet

Beginning CGI Programming with Perl

Exercise 2.2. Using the Perl regular array

In Exercise 1.2, “Using the Scalar Variable,” you learned that Perl contains three basic storage containers:

  • The scalar variable
  • A regular array
  • The associative array

In this exercise, you will learn about the Perl regular array storage container. Arrays enable you to store multiple items in a single, named area. The array is basically a file box. You can store all kinds of different things in an array, but in Perl you cannot store other arrays in an array. Listing 2.4 illustrates saving string or character data to a regular array. Take the time to type in this short program and, after you work through this lesson, spend some time modifying the data stored in the array. Try storing numbers and see what happens.


Listing 2.4. Perl’s array storage container.

01: #!/usr/local/bin/perl02:03: @my_name = ("Eric","C.","Herrmann");04:05: $myName[0] = "Scott";06: $myName[1] = "E.";07: $myName[2] = "Herrmann";08:09: print "Hello World @my_name wrote this\n";10: print "Really @myName wrote this\n";11: print "No Kidding $my_name[0] $my_name[1] $my_name[2] wrote this!\n";

On line 3 in this listing, you store your entire name in the file box or array @my_name. The double quotation marks are required. Perl has a little fun with the novice programmer when printing arrays. If you print this array like this,

print @my_name;

you get the following:

ERIcc.HERRMANN

If you use this format,

print "@my_name";

you get

ERIC C. HERRMANN

Perl puts spaces around names in arrays when the array is printed inside double quotation marks.

Remember that, in Perl, all arrays start with the at (@) sign. So you should be confused by lines 5-7. These lines do exactly the same thing as line 3, except they store the data one piece at a time. Each piece of data is stored in an array cell. You can think of each array cell as a shoe box or some type of scalar object.

Only one piece of information can be stored in an array cell at a time. Or, in programming tech speak, An array cell is a scalar variable; therefore, only one data object can be stored in it at a time. I really don’t think tech speak is intended to make things impossible for the novice to understand. It helps those trained in the field to speak more precisely, but it sure is a pain if someone tries to explain a concept in a language you don’t understand. I must admit, however, to knowing a few self-important people who use tech speak to keep their egos inflated. Here, I’m really trying to be precise about a very important concept.

The storage of data on lines 5-7 is the storing of one piece of data at a time. The name or string on the right side of the assignment statement is a single piece of data, and $my_name[n] is the location or scalar variable into which that data will be stored. Just like regular scalar variables ($variable_name), array cells are created when you store something in them. The special variable $#array_name keeps track of how many array cells an array currently has. The next array cell to be created always will be $#array_name + 1. Tutorial 6 includes a section called “Using Perl’s Special Variables,” where you can learn more about some of Perl’s more important special variables, such as $#array_name.

On lines 9-11, you use some of the ways to get at array data. Line 9 shows how you can access the entire contents of an array just by using the at (@) sign. Line 9 prints the entire array just like it was defined on line 3. Line 10 prints the array created on lines 5-7. This should help you understand that there is no real difference in the way the two variables (@my_name and @myName) or arrays are created.

Finally, line 11 shows you the most common way to get data out of an array. Line 11 prints the array created on line 3 one array cell at a time.

Hopefully, seeing the same data being stored and accessed in different ways will help you understand how arrays work. Because each array cell is really a scalar variable, you can access the data just like any other scalar data. First, you begin the scalar variable name with a dollar sign ($), just like any other scalar variable. Next, because the data is stored in an array, you need to tell Perl that the variable is an array. You do this by adding the square brackets ([]) to the variable name. Finally, you must tell Perl which array cell contains the scalar variable. This is done by putting a number between the square brackets. The number defines a particular scalar variable or array cell for Perl.

So when you want to use the data stored in an array, put a dollar sign before the array name, square brackets after the array name, and the array cell number between the square brackets.

Remember that arrays generally start storing data at array cell 0. This means that if there is one piece of data in the array, it will be at $array_name[0]. The $#array_name variable will be set to 0. If there were two pieces of data stored in the first two array cells, they would be stored at $array_name[0] and $array_name[1], and $#array_name would be equal to 1. This usually confuses anyone who isn’t familiar with this convention, so don’t be upset if it messed you up the first few times. Arrays generally start counting from 0, so $#array_name contains one less than the number of array cells but can be used to access the last array cell.