References and Nested Data
Array of Hashes
An array reference can hold hash references for small table-like data.
Array of Hashes
array_of_hashes.pl
use strict;
use warnings;
my $target = ;
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 = ;
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 = ;
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";
row lookup
A row lookup walks entries until it finds the row with the needed value.