Array of Hashes in Perl
The following code snippet provides the often-saught Array of Hashes in Perl. This is my take on the Array of Hashes exercise from the book of Intermediate Perl, also known as the Alpaca book.
my %gilligan_info = (
name => 'Gilligan',
hat => 'White',
shirt => 'Red',
position => 'First Mate',
location => 'The Island',
);
my %skipper_info = (
name => 'Skipper',
hat => 'Black',
shirt => 'Blue',
position => 'Captain',
location => 'The Island',
);
my %mr_howell_info = (
name => 'Mr. Howell',
hat => 'Khaki',
shirt => 'Bone',
position => 'First Mate',
location => 'The Island',
);
my %mrs_howell_info = (
name => 'Mrs. Howell',
hat => 'Blue',
shirt => 'Pink',
position => 'Captain',
location => 'The Island',
);
my @crew = (\%gilligan_info,\%skipper_info,\%mr_howell_info,\%mrs_howell_info);
my $format = "%-15s %-7s %-7s %-15s %-15s\n";
foreach my $crewmember (@crew) {
printf $format, @$crewmember{qw(name shirt hat position location)};
}
Written by Jason McVeigh
Related protips
2 Responses
I made this!
over 1 year ago
·
I ♥ what I do and I hope you do too!
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Perl array hashes
Authors
jmcveigh
4.458K
Related Tags
#perl array hashes
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#