- my @skipper = qw(blue_shirt hat jacket preserver sunscreen);
-
my @skipper_with_name = ('Skipper', \@skipper);
-
my @professor = qw(sunscreen water_bottle slide_rule batteries radio);
-
my @professor_with_name = ('Professor', \@professor);
-
my @gilligan = qw(red_shirt hat lucky_socks water_bottle);
-
my @gilligan_with_name = ('Gilligan', \@gilligan);
-
-
-
my @all_with_names = (
-
\@skipper_with_name,
-
\@professor_with_name,
-
\@gilligan_with_name,
-
);
-
-
print "${$all_with_names[1]}[0]\n";
-
print "@{$all_with_names[1]}\n";
#Professor
#Professor ARRAY(0x8c351ec)
As with array references, we can use shortcuts to replace the
complex curly-braced forms under some circumstances. For example, if the only
thing inside the curly braces is a simple scalar variable (as shown in these
examples so far), we can drop the curly braces:
Like an array reference, when referring to a specific hash
element, we can use an arrow form:
- my %gilligan_info = (
-
name => 'Gilligan',
-
hat => 'White',
-
shirt => 'Red',
-
position => 'First Mate',
-
);
-
my %skipper_info = (
-
name => 'Skipper',
-
hat => 'Black',
-
shirt => 'Blue',
-
position => 'Captain',
-
);
-
my @crew = (\%gilligan_info, \%skipper_info);
-
-
-
-
Thus, $crew[0] is a hash reference to the information about Gilligan. We can get to Gilligan's name via any one of:
-
-
${ $crew[0] } { 'name' }
-
my $ref = $crew[0]; $$ref{'name'}
-
$crew[0]->{'name'}
-
$crew[0]{'name
my $format = "%-15s %-7s %-7s %-15s\n";
printf $format, qw(Name Shirt Hat Position);
for my $crewmember (@crew) {
printf $format,
$crewmember->{'name'},
$crewmember->{'shirt'},
$crewmember->{'hat'},
$crewmember->{'position'};
}