KindergartenGarden.pm (905B) - raw
1 package KindergartenGarden; 2 use strict; 3 use warnings; 4 use Exporter qw<import>; 5 use Data::Dumper; 6 our @EXPORT_OK = qw<plants>; 7 8 sub plants { 9 # $input is a reference to a hash with two keys: diagram and student 10 # both diagram and student are scalar strings 11 my ($input) = @_; 12 my $diagram = $input->{diagram}; 13 my $student = $input->{student}; 14 my @rows = split /\n/, $diagram; 15 my @children = qw( Alice Bob Charlie David Eve Fred Ginny Harriet Ileana Joseph Kincaid Larry); 16 my @positions = map { $_ * 2 } (0 .. scalar(@children) - 1); 17 my %child_pos; 18 @child_pos{ @children } = @positions; # hash slice 19 my %plants = qw( G grass C clover R radishes V violets); 20 my @student_plants; 21 foreach (@rows) { 22 push @student_plants, $plants{substr($_, $child_pos{$student}, 1)}; 23 push @student_plants, $plants{substr($_, $child_pos{$student} + 1, 1)}; 24 } 25 return \@student_plants; 26 } 27 28 1;