Arrays
Learn about arrays in Perl.
Perl’s array data type is a language-supported aggregate that stores zero or more scalars. We can access individual members of the array by integer indexes, and we can add or remove elements at will. Arrays grow or shrink as we manipulate them.
The @
sigil denotes an array. To declare an array, we write this:
my @items;
Array elements
We use the scalar sigil to access an individual element of an array. $cats[0]
refers
unambiguously to the @cats
array, because postfix square
brackets []
always mean indexed access to an array.
The first element of an array is at the zeroth index:
# @cats contains a list of Cat objects
my $first_cat = $cats[0];
The last index of an array depends on the number of elements in the array. An array in scalar context (due to scalar assignment, string concatenation, addition, or boolean context) evaluates to the number of elements in the array:
Get hands-on with 1200+ tech skills courses.