Nested data structures
Array of hashes
Records usually become an array of hash references: the array keeps the order, and each hash holds the fields of one row. The arrow between the two indexes is the only new syntax you need.
my @users = ( { name => "Ada", age => 36 }, { name => "Grace", age => 45 }, ); print $users[1]{name}, "\n"; # Grace
perl demo.pl
Grace
Between two subscripts the arrow may be omitted, so $users[1]{name} is the same as $users[1]->{name}. Keep the arrow when a reference is involved and the reader will thank you.
Hash of arrays
The mirror shape groups several values under one key. A hash of array references is the natural way to store, say, all log lines per host.
my %by_host = ( db1 => ["ok", "ok"], db2 => ["fail"], ); push @{$by_host{db2}}, "ok"; print @{$by_host{db2}}, "\n"; # failok, print adds no separator
perl demo.pl
failok
The braces around $by_host{db2} tell Perl where the key ends before it dereferences the array. Writing @$by_host{db2} would mean something else entirely, a hash slice.
Walk nested data
Two nested loops read any array of hashes. Name the row variable and the fields stay obvious even in a long block.
my @users = ({ name => "Ada", age => 36 }, { name => "Grace", age => 45 }); my %by_host = (db1 => ["ok", "ok"], db2 => ["fail", "ok"]); for my $user (@users) { print "$user->{name} is $user->{age}\n"; } for my $host (sort keys %by_host) { print "$host: ", scalar @{$by_host{$host}}, "\n"; }
perl demo.pl
Ada is 36
Grace is 45
db1: 2
db2: 2
Inspect with Data::Dumper
Printing a reference shows something like HASH(0x55d1c8), which tells you nothing. Data::Dumper is in the core, so it is always available, and it prints the whole structure as Perl source.
my @users = ({ name => "Ada", age => 36 }, { name => "Grace", age => 45 }); use Data::Dumper; $Data::Dumper::Sortkeys = 1; print Dumper(\@users);
perl demo.pl
$VAR1 = [
{
'age' => 36,
'name' => 'Ada'
},
{
'age' => 45,
'name' => 'Grace'
}
];
$Data::Dumper::Sortkeys = 1; fixes the key order, because a hash hands out its keys in a different order on every run. Leave it out and the same script prints the fields of each record in a new sequence each time.
Shallow and deep copy
Assigning a reference copies the pointer, not the data: both names then change the same structure. Copy the top level with [@list] or {%hash}, and use Storable for a full deep copy.
my @users = ({ name => "Ada", age => 36 }, { name => "Grace", age => 45 }); use Storable qw(dclone); my $copy = dclone(\@users); $copy->[0]{age} = 99; print $users[0]{age}, "\n"; # 36, original untouched
perl demo.pl
36
Shallow copies are enough when the nested references are never modified. Reach for dclone the moment you plan to write into a structure that someone else also holds.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
How is a list of records usually stored in Perl?
Answer
As an array of hash references: the array keeps the order, each hash holds one row
The array gives the rows an order and each hash reference holds the named fields of one row.
-
Is
$users[1]{name}the same as$users[1]->{name}?Answer
Yes: between two subscripts the arrow may be omitted
Perl lets the arrow go when one subscript follows another, though keeping it can help the reader.
-
Why does
push @{$by_host{db2}}, "ok";need the braces?Answer
The braces mark where the key ends before the array is dereferenced
Without them
@$by_host{db2}reads as a hash slice, which is a different operation on a different variable. -
What does
@$by_host{db2}mean?Answer
A hash slice of one key
The at sign with a key list takes values out of the hash as a list, which is why the braced form exists for the array inside.
-
What does printing a reference on its own show?
Answer
Something like
HASH(0x55d1c8), which tells you nothing about the dataA reference is an address; Data::Dumper prints the data it points at, as Perl source.
-
What does
my $copy = { %$r };copy?Answer
Only the top level: the nested references are still shared
A shallow copy makes a new hash of the same values, and a nested reference in it points at the original data;
dclonecopies deeply. -
What does this program print?
my @u = ({ name => "Ada" }, { name => "Grace" }); print $u[1]{name}, "\n";Answer
Grace
Index 1 is the second record, and the key reads its name field.
-
What does this program print?
my %h = (db1 => ["ok", "ok"]); push @{$h{db1}}, "fail"; print scalar @{$h{db1}}, "\n";Answer
3
The push adds one element to the array inside the hash, which now holds three.
-
What does this program print?
my $r = { a => 1 }; my $copy = { %$r }; $copy->{a} = 2; print "$r->{a} $copy->{a}\n";Answer
1 2
The shallow copy is a new hash, so writing to it leaves the original at 1.
-
What does this program print?
my @u = ({ n => 1 }, { n => 2 }); print scalar @u, "\n";Answer
2
The array has two elements, and each element happens to be a hash reference; the count is still 2.
Article author: Arthur Isaev