perl - $array can't print anything -
this program , want let user type matrix line line , print while matrix , can't see matrix
the user type
1 2 3 4 5 6 7 8 9
like this
and want let show
1 2 3 4 5 6 7 8 9
perl program
$num = 3; while($num > 0 ) { $row = <stdin>; $row = chomp($row); @row_array = split(" ",$row); push @p_matrix , @row_array; @row_array = (); $num = $num - 1; } for($i=0;$i<scalar(@p_matrix);$i++) { for($j=0;$j<scalar(@p_matrix[$i]);$j++) { printf "$d ",$p_matrix[$i][$j]; } print "\n"; }
i change expression => printf "$d ",$p_matrix[$i][$j];
print $p_matrix[$i][$j]
still don't work.
to create multi-dimensional array, have use references. use
push @p_matrix, [ @row_array ];
to create desired structure.
also, chomp
not return modified string. use
chomp $row;
to remove newline $row. moreover, chomp
not needed @ if split
on ' '
.
printf
uses %
formatting character, not $
.
you can use data::dumper
inspect complex data structures. use strict
, warnings
avoid common problems. here how write program:
#!/usr/bin/perl use warnings; use strict; use data::dumper; @p_matrix; push @p_matrix , [ split ' ' ] while <>; warn dumper \@p_matrix; $i (0 .. $#p_matrix) { $j (0 .. $#{ $p_matrix[$i] }) { printf '%d ', $p_matrix[$i][$j]; } print "\n"; }
Comments
Post a Comment