Arrays of Arrays.
#!/usr/bin/perl -w
use strict;
my @AoA = (
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
);
# accessing arrays of arrays using index
for my $i ( 0 .. $#AoA ) {
for my $j ( 0 .. $#{$AoA[$i]} ) {
print $AoA[$i][$j] . "\t";
}
print "\n";
}
# accessing arrays of arrays
for my $row ( @AoA ) {
for my $col ( @{$row} ) {
print $col . "\t";
}
print "\n";
}
Arrays of Hashes.
#!/usr/bin/perl -w
use strict;
my @AoH;
push @AoH, { a => 1, b => 2 };
push @AoH, { c => 3, d => 4 };
for my $i ( 0 .. $#AoH ) {
for my $k ( keys %{$AoH[$i]} ) {
print $k . ":" . ${$AoH[$i]}{$k} . " ";
}
print "\n";
}
Hashes of Hashes.
#!/usr/bin/perl -w
use strict;
my %HoH;
$HoH{"keyA"} = { a => 1, b => 2 };
$HoH{"keyB"} = { c => 3, d => 4 };
for my $kA ( keys %HoH ) {
print $kA . ":\n";
for my $kB ( keys %{$HoH{$kA}} ) {
print $kB . ":" . $HoH{$kA}{$kB} . " ";
}
print "\n";
}
Update:Links
No comments:
Post a Comment