package KindergartenGarden; use strict; use warnings; use Exporter qw; use Data::Dumper; our @EXPORT_OK = qw; sub plants { # $input is a reference to a hash with two keys: diagram and student # both diagram and student are scalar strings my ($input) = @_; my $diagram = $input->{diagram}; my $student = $input->{student}; my @rows = split /\n/, $diagram; my @children = qw( Alice Bob Charlie David Eve Fred Ginny Harriet Ileana Joseph Kincaid Larry); my @positions = map { $_ * 2 } (0 .. scalar(@children) - 1); my %child_pos; @child_pos{ @children } = @positions; # hash slice my %plants = qw( G grass C clover R radishes V violets); my @student_plants; foreach (@rows) { push @student_plants, $plants{substr($_, $child_pos{$student}, 1)}; push @student_plants, $plants{substr($_, $child_pos{$student} + 1, 1)}; } return \@student_plants; } 1;