# File: matrix0.pl # Title: Matrices in Perl. # Matrices are lists of arrays. Array names begin with @. # Author: Mihaela Malita # C:\myPerl>perl matrix0 # 7 # 22 # Length Matrix M= 3 # 22 3 5 # 3 5 7 # 5 7 3 # Declare a matrix M # use strict; use warnings; @M = ( [7,3,5], [3,5,7], [5,7,3]); # 3x3 matrix print $M[0][0],"\n"; #print corner: 7 $M[0][0] = 22; # change value to first corner print $M[0][0]; #print corner: 22 $len = @M; # number of lines = 3 print "\nLength Matrix M= ", $len,"\n"; # length array printMatrix(@M); exit; ### Subroutine to print a Matrix (N x N) sub printMatrix { my(@M) = @_ ; for ($i = 0 ; $i < @M ; ++$i) { for ($j = 0 ; $j < @M ; ++$j) { print $M[$i][$j], "\t"; #print each element on a different line } print "\n"; } return; }