Last Updated: July 15, 2016
·
4.459K
· jmcveigh

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)};
}

2 Responses
Add your response

I made this!

over 1 year ago ·

I ♥ what I do and I hope you do too!

over 1 year ago ·