I have a small script to multiply two matrices together (and no this is not homework, I'm just puttering around with Perl). I have a problem with it that I have been unable to resolve.
matrix_count_rows_cols($r_mat1) is consistently returning 0 rows and 0 columns. Not sure why. Any ideas? (Please keep in mind I'm still pretty green when it comes to Perl). Thanks in advance

use strict 'vars'; use strict 'subs'; use warnings; my @matrix1=( [1, 2, 3], [4, 5, 6], [7, 8, 9] ); my @matrix2=( [2, 4, 6], [1, 3, 5], [7, 8, 9] ); print "matrix1=\n"; for (my $i=0;$i le 2;$i++) { for (my $j=0;$j le 2;$j++) { print $matrix1[$i][$j]," "; } print "\n"; } print "matrix2=\n"; for (my $i=0;$i le 2;$i++) { for (my $j=0;$j le 2;$j++) { print $matrix2[$i][$j]," "; } print "\n"; } my @product=matrix_multiply(@matrix1,@matrix2); print "product=\n"; for (my $i=0;$i le 2;$i++) { for (my $j=0;$j le 2;$j++) { print $product[$i][$j]," "; } print "\n"; } sub matrix_multiply { my ($r_mat1,$r_mat2)=@_; my ($r_product); my ($r1,$c1)=matrix_count_rows_cols($r_mat1); my ($r2,$c2)=matrix_count_rows_cols($r_mat2); print $c1,$c2,"\n"; print $r1,$r2,"\n"; die "matrix 1 has $c1 columns and matrix 2 has $r2 rows>" . " Cannot multiply\n" unless ($c1==$r2); for (my $i=0;$i<$r1;$i++) { for (my $j=0;$j<$c2;$j++) { my $sum=0; for (my $k=0;$k<$c1;$k++) { $sum+=$r_mat1->[$i][$k]*$r_mat2->[$k][$j]; } $r_product->[$i][$j]=$sum; } } $r_product; } sub matrix_count_rows_cols { my ($r_mat)=@_; my $num_rows=@$r_mat; my $num_cols=@{$r_mat->[0]}; ($num_rows,$num_cols); }

In reply to Matrix Multiplication Problem by RolandGunslinger

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.