Perl Scripts ·
Multidimensional Arrays in Perl: Build, Read and Loop
By Manu Mathew, Technical Lead Engineer · Published 8 October 2013 · Last updated 5 October 2026

Sooner or later a Perl script needs a table: rows from a report, a grid of values, or a list of records. This page brings together several short notes on multidimensional arrays in Perl I wrote between 2013 and 2016. I have rewritten them in one place, with tested examples and current advice, so you don’t have to piece the answer together from fragments.
Quick answer
Perl has no true multidimensional arrays. Instead, each element of an outer array holds a reference to an inner array. Write [ … ] to make an inner array, read an entry with $table[1][0], and loop with @{ $table[$i] }. Perl creates missing inner arrays for you when you push onto them.
I tested each example with Perl 5.40, using use strict and use warnings.
How do I build a two-dimensional array in a loop?
Push values onto @{ $grid[$row] }; Perl creates each inner array the first time you use it (this is called autovivification).
use strict;
use warnings;
my @grid;
for my $row (0 .. 3) {
for my $col (0 .. 4) {
push @{ $grid[$row] }, $row * 10 + $col; # autovivifies the inner array
}
}
print $grid[3][2], "\n"; # 32
How do I write a two-dimensional array directly?
Use square brackets for each row. They create an anonymous array and return a reference to it.
use strict;
use warnings;
my @table = (
[1, 2, 3, 'four'],
['one', 'two', 'three', 4],
);
print $table[1][0], "\n"; # one
print $table[1]->[0], "\n"; # one (the arrow is optional between subscripts)
my $ref = \@table; # a reference to the whole table
print $ref->[0][3], "\n"; # four
$table[1][0] and $table[1]->[0] mean the same thing: between two subscripts the arrow is optional. You only need it after a plain reference, as in $ref->[0][3]. You can also build rows from named arrays with (\@row1, \@row2), but the rows then change if those arrays change.
How do I loop over every row and count the cells?
scalar(@table) gives the number of rows, and scalar @{ $table[$i] } the number of cells in row $i.
use strict;
use warnings;
my @table = (
['a', 'b'],
['c', 'd', 'e'],
);
print "Rows: ", scalar(@table), "\n"; # Rows: 2
for my $i (0 .. $#table) {
my $cells = scalar @{ $table[$i] };
print "Row $i has $cells cells: @{ $table[$i] }\n";
}
# Row 0 has 2 cells: a b
# Row 1 has 3 cells: c d e
Rows don’t have to be the same length, which is handy for ragged data. If you need to see the whole structure while debugging, use Data::Dumper; print Dumper(\@table); prints it.
Frequently asked questions
Is an array of arrays the same as a two-dimensional array?
For most purposes, yes. Perl stores each row as a separate array and keeps references to them in the outer array, so rows can have different lengths. Reading $table[$row][$col] works just like a two-dimensional array in other languages.
How do I sort a two-dimensional array by one column?
Sort the rows with a block that compares that column, for example sort { $a->[1] <=> $b->[1] } @table for numbers in the second column. Use cmp instead of <=> for text. Whole rows move, so each row’s cells stay together.
How do I copy a two-dimensional array without sharing rows?
Copy each row as well as the outer array: my @copy = map { [ @$_ ] } @table;. A plain my @copy = @table copies only the references, so changing a cell in the copy would change the original too.
When should I use a hash of arrays instead?
When you look rows up by a name or ID rather than by position. A hash such as $orders{$customer}, holding a reference to an array of orders, is easier to read and quicker to search than scanning every row of an array.
Related notes: Perl arrays: length, add, remove and search; Perl hashes; sorting arrays in Perl; hiring a remote Perl developer.
Need help with Perl code?
If you have Perl code that needs fixing, extending or upgrading, I’m happy to take a look. I work remotely from Kozhikode, Kerala, with teams across India and elsewhere; Perl development and maintenance explains how I help. If you are deciding how to bring someone in, Hiring a remote Perl developer is a short checklist.
You can reach me through the contact section or email me at manu@manu.co.in. A few lines about the system and what you need is a good start.
Sources: perllol: manipulating arrays of arrays in Perl; perlref: Perl references; perldsc: Perl data structures cookbook.