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, ...

Get Sams Teach Yourself Perl in 21 Days, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.