I liked
stevieb's post. Here is something else to consider:
#!/usr/bin/perl -w
use strict;
## An Array of Hash is the most similar Perl
## way of representing a 'C' array of structure
my @classAoH = (
{
name => 'Bob',
grade => 'C',
},
{
name => 'Jane',
grade => 'A',
}
);
## An Array of Array can represent the same
## data, but we have to use an index number instead
## of a hash key to access the data
my @classAoA = (
[ 'Bob' , 'C'],
[ 'Jane', 'A'],
);
foreach my $hash_reference (@classAoH)
{
print "$hash_reference->{name} $hash_reference->{grade} \n";
}
foreach my $array_reference (@classAoA)
{
print "$array_reference->[0] $array_reference->[1]\n";
}
__END__
Both foreach() loops print exactly same thing:
Bob C
Jane A
The order is guaranteed because both are arrays
of "something".
The difference is that often {name} or {grade}
means something more and is easier to understand in
the rest of the code for the reader than [0] or [1].
So, "readability" is something to consider.
Of course this is not "free".
The AoH is less efficient than the AoA, but often
that does not matter. Often the most important thing
is understandability and maintainability and the AoH
wins on that point.