package Matrix; use strict; use warnings; use Exporter qw; our @EXPORT_OK = qw; sub define_matrix { my @matrix; # shift is just the parameter that was passed to the subroutine # split on the newline my @lines = split /\n/, shift; # push into the matrix a reference to an array which represents # each row. Elements of the array are found by splitting on # blank space push @matrix, [split / /] foreach (@lines); return @matrix; } # $input is a reference to a hash with two keys: # index, which asks for a row or column, and # string, which represents the matrix sub row { my ($input) = @_; my $index = $input->{'index'}; my @matrix = define_matrix($input->{'string'}); # since we are returning a row, $index-1 is the row within @matrix # we need to return. In this case, we are returning a reference to # an array which contains the element of the row return $matrix[$index - 1]; } sub column { my ($input) = @_; my $index = $input->{'index'}; my @matrix = define_matrix($input->{'string'}); my @column; # each row of the matrix is represented by an array. $row is a # reference to that array. To get the column, we use [$index-1]. foreach my $row (@matrix) { push @column, $row->[$index - 1]; } # we need to return a reference to the array containing the column return \@column; } 1;