References and Nested Data
Array of Hashes
An array reference can hold hash references for small table-like data.
row lookup
A row lookup walks entries until it finds the row with the needed value.
Array of Hashes
array_of_hashes.pl
Replay: real traced execution (multi-file project)
use strict;
use warnings;
my $target = "beta";
my $rows_ref = [
{ name => "alpha", score => 3 },
{ name => "beta", score => 5 },
];
my $found = "no";
my $score = 0;
foreach my $row_ref (@{$rows_ref}) {
if ($row_ref->{name} eq $target) {
$found = "yes";
$score = $row_ref->{score};
}
}
print "target=$target\n";
print "found=$found\n";
print "score=$score\n";
use strict;
use warnings;
my $target = "alpha";
my $rows_ref = [
{ name => "alpha", score => 3 },
{ name => "beta", score => 5 },
];
my $found = "no";
my $score = 0;
foreach my $row_ref (@{$rows_ref}) {
if ($row_ref->{name} eq $target) {
$found = "yes";
$score = $row_ref->{score};
}
}
print "target=$target\n";
print "found=$found\n";
print "score=$score\n";
use strict;
use warnings;
my $target = "gamma";
my $rows_ref = [
{ name => "alpha", score => 3 },
{ name => "beta", score => 5 },
];
my $found = "no";
my $score = 0;
foreach my $row_ref (@{$rows_ref}) {
if ($row_ref->{name} eq $target) {
$found = "yes";
$score = $row_ref->{score};
}
}
print "target=$target\n";
print "found=$found\n";
print "score=$score\n";
$target ← beta, $found ← no, $score ← 0
4my $target→ beta = "beta"; #@target="alpha", "gamma"5my $rows_ref = [6 { name => "alpha", score => 3 },7 { name => "beta", score => 5 },8];9my $found→ no = "no";10my $score→ 0 = 0;foreach my $row_ref (@{$rows_ref})
pass 1 of 212foreach my $row_ref (@{$rows_ref}) {13 if ($row_ref->{name} eq $target) {foreach my $row_ref (@{$rows_ref})
pass 2 of 212foreach my $row_ref (@{$rows_ref}) {13 if ($row_ref->{name} eq $target) {$found ← yes, $score ← 5
12foreach my $row_ref (@{$rows_ref}) {13 if ($row_ref->{name} eq $targetbeta) {14 $found→ yes = "yes";15 $score→ 5 = $row_ref->{score};16 }print "target=$target ";
19print "target=$targetbeta\n";20print "found=$foundyes\n";21print "score=$score5\n";outputtarget=beta found=yes score=5
$target ← alpha, $found ← no, $score ← 0
4my $target→ alpha = "alpha";5my $rows_ref = [6 { name => "alpha", score => 3 },7 { name => "beta", score => 5 },8];9my $found→ no = "no";10my $score→ 0 = 0;foreach my $row_ref (@{$rows_ref})
pass 1 of 212foreach my $row_ref (@{$rows_ref}) {13 if ($row_ref->{name} eq $target) {$found ← yes, $score ← 3
12foreach my $row_ref (@{$rows_ref}) {13 if ($row_ref->{name} eq $targetalpha) {14 $found→ yes = "yes";15 $score→ 3 = $row_ref->{score};16 }foreach my $row_ref (@{$rows_ref})
pass 2 of 212foreach my $row_ref (@{$rows_ref}) {13 if ($row_ref->{name} eq $target) {print "target=$target ";
19print "target=$targetalpha\n";20print "found=$foundyes\n";21print "score=$score3\n";outputtarget=alpha found=yes score=3
$target ← gamma, $found ← no, $score ← 0
4my $target→ gamma = "gamma";5my $rows_ref = [6 { name => "alpha", score => 3 },7 { name => "beta", score => 5 },8];9my $found→ no = "no";10my $score→ 0 = 0;foreach my $row_ref (@{$rows_ref})
pass 1 of 212foreach my $row_ref (@{$rows_ref}) {13 if ($row_ref->{name} eq $target) {foreach my $row_ref (@{$rows_ref})
pass 2 of 212foreach my $row_ref (@{$rows_ref}) {13 if ($row_ref->{name} eq $target) {print "target=$target ";
19print "target=$targetgamma\n";20print "found=$foundno\n";21print "score=$score0\n";outputtarget=gamma found=no score=0