Going Deeper

We've covered a lot in this lesson, but there's still more about arrays and hashes I haven't discussed (really!). This section summarizes some of the features of lists, arrays, and hashes that I haven't covered in the body of this lesson. Feel free to explore these parts of Perl on your own.

Negative Array Indexes

In the array access expression $array[index], usually the index will be the position of the element in the array, starting from 0. You can also use negative array subscripts, like this:

$array[-1];

Negative array subscripts will count back from the end of the array—so an index of -1 refers to the last index in the array (same as $#array), -2 refers to the second to last index, and so on. You can also assign to those positions, although it might be a better idea to use syntax that's more explicit and easier to read in that instance.

More About Ranges

Earlier in this lesson, we used the range operator .. to create a list of numeric elements. Ranges also have several features I didn't mention in that section. For example, you can use ranges with characters, and the range will generate a list of all the characters in the ASCII character set between the operands. For example, the range 'a' .. 'z' results in a list of 26 characters from a to z.

You can also use this behavior in various magical ways, combining numbers and letters or multiple letters, and the range will happily oblige with values between the upper and lower values.

The range operator can also be used in a scalar context, and returns a boolean value. The perlop man page describes it as “The operator is bistable, like a flip-flop, and emulates the line-range (comma) operator of sed, awk, and various editors.” I suppose if you have worked with sed or awk and understand what this means, you can use the range operator in this way. (I don't, and I haven't.) There is also a three-dot range operator (…) which more closely emulates sed's behavior. See the perlop man page under Range Operator for more information and examples.

chomp and chop on Lists

The chomp and chop functions, to remove newlines or characters from the end of strings, also work with lists as their arguments. On lists, they work through each element in the list and remove the newline or the last character from each element. This could be useful for removing all the newline characters from input you read from a file into a list.

See the perlfunc man page for more information on chomp and chop.

Output Field, Record and List Separators

Part of Perl's built-in library is a set of global variables that can be used to modify Perl's behavior in many situations. You'll learn about many of these variables as this book progresses, or you can see the perlvar man page for a list of them all.

Relevant to the discussion today are the output-field, output-record, and list-separator variables. These three global variables can be set to change the default way that Perl prints lists. Table 4.1 defines these variables.

Table 4.1. Output Global Variables
Variable Name What it does
$, Output field separator; the characters to print in between list elements. Empty by default.
$ Output record separator; the characters to print at the end of a list. Empty by default.
$" Same as the output field separator, except only for list variables interpolated inside strings. A single space by default.

As you learned in the section “Printing Lists,” when you print a bare list, Perl will concatenate all the values in the list together:

print (1,2,3);  # prints "123";

In reality, Perl actually prints the elements of the list with the value of the output field separator between the elements and the output record separator at the end. Because both those variables are empty by default, you get the previous behavior. You could set those variables to get different printing behavior:

$, = '*';
$= "
";
print (1,2,3);  # prints "1*2*3
"

The list field separator behaves similarly to the output field separator, but only when you use a list variable inside a string. By default the list field separator contains a string—and that's the default printing behavior for list variables interpolated into strings. Change the list field separator to change the printing behavior for array variables inside strings.

Void Context

In addition to list, scalar, and boolean context, Perl also has a void context, which is defined as simply a place where Perl doesn't expect anything. You'll most likely see this in warnings and errors when you included something where Perl didn't expect it— “unexpected constant in void context,” for example.

..................Content has been hidden....................

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