in reply to simple matrix
Is matrix a reference to an array (@{$matrix}) or an array (@matrix). You've used both. use strict is your friend. It would have caught that error.
Also, you're always pushing a reference to the same variable (@row) over and over again. All rows of your matrix are going to be the same. The following creates multiple row variables (all named @row). The alternative would be to copy @row into a newly create anon aray by replacing \@row with [ @row ].
#! /usr/bin/perl use strict; use warnings; use Data::Dumper; my $a = 1; my $b = 2; my $c = 3; # It's bad to use $a and $b my $d = 4; my $e = 5; my $f = 6; my $g = 7; my $h = 8; my $i = 9; my @matrix; { my @row; push(@row, $a); push(@row, $b); push(@row, $c); print "@row\n"; push(@matrix, \@row); } { my @row; push(@row, $d); push(@row, $e); push(@row, $f); print "@row\n"; push(@matrix, \@row); } { my @row; push(@row, $g); push(@row, $h); push(@row, $i); print "@row\n"; push(@matrix, \@row); } print(Dumper(\@matrix));
or
my @matrix = ( [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ], );
|
|---|