Perl arrays

Something that we can make use of in Perl is an array. These arrays are variables that are created from lists; put simply, they are basically multi-valued variables. If we were to use a container analogy to describe a variable, it will be either a cup or a placeholder for one value. An array will be analogous to a crate. We can describe the crate with a single name but we have to include additional naming elements to access each slot within the crate. A crate can hold more than a single item, just like an array.

We saw that by using bash scripting we can pass command line arguments in the script. The arguments were using their own variable name, $1, $2, and so on. This also clashed with the name of the program, to a degree, because of the fact that it was $0. Even though they may appear similar, there is no logical relationship between $0 and $1. The $0 variable is the name of the script and $1 is the first argument. When we look at this in Perl, we can start to see some of the major differences.

Program name?

The program name in Perl can still be accessed with the $0 variable. We can see this in the following script:

#!/usr/bin/perl
print("You are using $0
");
print("Hello World
");

Now, even though we think of $0 as quite simple to use, as we have accessed it in bash before, if we approach this with fresh eyes it is not so obvious. Perl has a module called English where we have some more friendly names defined for many other variables used in Perl. If we take a look at the following script we can see this in use:

#!/usr/bin/perl
use English;
print("You are using $PROGRAM_NAME
");
print("Hello World
");

The line use English; will import the module that redefines $0 so that it can be referenced as $PROGRAM_NAME. Although, this requires more typing it also acts as a better name documenting its purpose.

Argument arrays

Rather than using $1, $2, and so on for the arguments; Perl now uses a list of arguments stored in a single array variable. The array name is @ARGV and we can access each argument supplied by this in the index number or slot number. The computers start counting at 0, so the first argument will be $ARGV[0], the second will be $ARGV[1], and so on.

Note

An index array is named using the @ symbol. Each element of the array is still a single or scalar variable and just like in bash, they are read with the $ symbol.

When we look at the following script, $HOME/bin/args.pl, we can see how to make the Hello script more portable by accepting arguments:

#!/usr/bin/perl
use English;
print("You are using $PROGRAM_NAME
");
print("Hello $ARGV[0]
");

We can see this in action by running the script, as shown in the following screenshot:

Argument arrays

Counting elements in an array

We can see that the command-line arguments are stored in the @ARGV array. We can count the number of arguments or, in fact, the elements in any array using the following code:

scalar @<array-name>;

So instead of using the $# to count the supplied arguments, we will use the code as follows:

scalar @ARGV;

If we add this to our script, it will be seen, as shown in the following code block:

#!/usr/bin/perl
use English;
print("You are using $PROGRAM_NAME
");
print("You have supplied: " . scalar @ARGV . " arguments
");
print("Hello $ARGV[0]
");

Note

We can also take a note from the previous code block that we can concatenate the output of a command with a test using the period character.

Looping through an array

In bash, we have a simple mechanism with $* to refer the list of arguments supplied to a script. In Perl, this is just a little different from having to loop through the list. However, the foreach keyword is built for this:

#!/usr/bin/perl
use English;
print("You are using $PROGRAM_NAME
");
print("You have supplied " . scalar @ARGV . " arguments
");
foreach $arg (@ARGV) {
 print("Hello $arg
");
}

We can see that the code is defined within the loop and is contained using the brace brackets. If you recall, bash did not specifically have a foreach keyword and it made use of do and done to constrain the code.

If we implement this code in the $HOME/bin/forargs.pl file, we can execute it as a code similar to the following screenshot:

Looping through an array

Creating arrays

So far, we have relied on the @ARGV system array and this has proved to be a great way to learn how to access and array. We now need to see how to create arrays of our own design.

Arrays are lists of values that can store mixed data types; so, there is no reason why we cannot have an array that stores both strings and numbers. The order in which the items are supplied to the array will set their index position. In other words, the first item in the list will be the first index or index 0 in the array. Consider the following code: $HOME/bin/array.pl:

#!/usr/bin/perl
use English;
print("You are using $PROGRAM_NAME
");
@user = ("Fred","Bloggs",24);
print("$user[0] $user[1] is @user[2]
");

The first thing we should notice is that when we are setting a variable of any type, including an array, we will use the designator for the variable type. We see here that the use of @user = …, will make use of the @ symbol as previously mentioned to denote that the variable is an array variable. If we were setting a scalar variable similar to the ones we use in bash, we will set $user. In bash, we do not use the designator when setting a variable and we cannot have spaces surrounding the assignment operator, =. Perl will allow the spaces and aids in the readability with an extra whitespace.

Next, we should notice that the list contains strings and an integer. This is perfectly acceptable and the array can hold different data types. A single name of the array makes sense, as we can now store related data into one object.

The final point to take note of in the supplied code is that we can easily concatenate the string values with integer values using Perl. There is no need to provide any form of data translation. Within the single string, we print the first name, last name, and age of the user.

On script execution, we should receive an output, as shown in the following screenshot:

Creating arrays
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.94.153