exercism-perl5

Repository for my Perl 5 Exercism exercises
git clone git://git.samirparikh.com/exercism-perl5
Log | Files | Refs

commit 162eb3e5be496c92c17899e418140ce7605ded4d
parent a96d9bbbe005722ef20e7d98d00a971b0db4d85b
Author: Samir Parikh <noreply@samirparikh.com>
Date:   Fri, 26 Nov 2021 22:59:05 +0000

add comments to Matrix.pm

Diffstat:
Mmatrix/Matrix.pm | 17++++++++++++++++-
1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/matrix/Matrix.pm b/matrix/Matrix.pm @@ -6,16 +6,28 @@ our @EXPORT_OK = qw<row column>; 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'}); - return \@{$matrix[$index - 1]}; + # 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 { @@ -23,9 +35,12 @@ sub column { 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; }