Arrays

Contents
Declaring a list
Index and sigil change
Slices
The last index
Ranges and qw

Declaring a list

An array holds an ordered list of scalars under a name with the at sign. The parentheses on the right are a list, not a grouping, so commas separate its elements.

my @langs = ("perl", "ruby", "python"); my @sizes = (10, 20, 30); print "@langs\n"; # perl ruby python

perl demo.pl

perl ruby python

The same name may hold an array and a scalar at once, because the sigil is part of the name. @langs and $langs are separate storage.

Index and sigil change

To read one element you use the dollar sigil, because a single element is a scalar. The brackets carry the index, counting from zero.

my @langs = ("perl", "ruby", "python"); print $langs[0], "\n"; # perl print $langs[2], "\n"; # python print $langs[-1], "\n"; # python, last element

perl demo.pl

perl
python
python

Negative indexes count back from the end, so the last element is always -1. That works for arrays and for slices, and it removes most of the off-by-one arithmetic from list code.

Slices

A slice takes several elements at once and keeps their order. The sigil becomes an at sign because the result is a list, and the brackets hold the indexes.

my @langs = ("perl", "ruby", "python"); my @first_two = @langs[0, 1]; print "@first_two\n"; # perl ruby my @last_two = @langs[-2, -1]; print "@last_two\n"; # ruby python

perl demo.pl

perl ruby
ruby python

The last index

The special form $#array gives the index of the last element, which is one less than the number of elements. Use scalar @array when you want the count itself.

my @langs = ("perl", "ruby", "python"); print $#langs, "\n"; # 2 print scalar @langs, "\n"; # 3

perl demo.pl

2
3

Assigning to $#array truncates or extends the array, so it is a quick way to keep the first ten elements: $#list = 9;

Ranges and qw

The range operator builds a list from two endpoints. Combined with qw, which splits a string on whitespace, it covers most literal lists in real code.

my @digits = (1 .. 5); my @letters = ("a" .. "e"); my @flags = qw(-v --quiet --dry-run); print "@digits\n"; # 1 2 3 4 5 print "@letters\n"; # a b c d e

perl demo.pl

1 2 3 4 5
a b c d e

Ranges are lazy about nothing: 1 .. 1_000_000 really builds a million elements. For big sequences use a counting loop from lesson 15 instead.

Next: push, pop, shift and splice

Article author: Arthur Isaev

Related articles
Perl
Perl course: zero to hero in 50 lessons
What is Perl and where it runs
Install Perl and check the version
Your first Perl script
The debugger as a REPL
Scalars and sigils
Numbers
Strings
Interpolation and quoting
Arrays
push, pop, shift and splice
Hashes
keys, values, exists and delete
Scalar and list context
Conditions and comparison
while, for, next and last
foreach and the default variable
Subroutines
Arguments in @_
References
Nested data structures
Regular expressions: match and anchors
Captures and named groups
Substitution with s///
split and join
grep and map
Sort and the Schwartzian transform
Read and write files
File tests and metadata
Directories and paths
die, warn and eval
Modules and Exporter
CPAN and cpanm
Objects with bless
Modern objects with Moo
Testing with Test::More
strict, warnings and diagnostics
Options with Getopt::Long
Environment and %ENV
system, backticks and pipes
One-liners with -n and -p
CSV and TSV processing
JSON with JSON::PP
HTTP clients with HTTP::Tiny
Sockets and IO::Socket
Databases with DBI
Capstone: todo CLI
Capstone: log report
Performance and profiling
Modern Perl tour
Hero roadmap: the whole course on one page
Install Perl on Windows 11
Install Perl on Ubuntu
Install Perl on Rocky Linux
Install Perl on macOS
Install Perl on FreeBSD
Perl in Docker
Banner image

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: