Reputation: 153
I have the following code which reads in a 6x6 array from STDIN and saves it as an array of anonymous arrays. I am trying to print out each element with $arr[i][j]
, but the code below isn't working. It just prints out the first element over and over. How am I not accessing the element correctly?
#!/user/bin/perl
my $arr_i = 0;
my @arr = ();
while ($arr_i < 6){
my $arr_temp = <STDIN>;
my @arr_t = split / /, $arr_temp;
chomp @arr_t;
push @arr,\@arr_t;
$arr_i++;
}
foreach my $i (0..5){
foreach my $j (0..5){
print $arr[i][j] . "\n";
}
}
Upvotes: 0
Views: 61
Reputation: 2062
To demonstrate the Perlish mantra that there's "more than one way to do it":
use 5.10.0; # so can use "say"
use strict;
use warnings qw(all);
sub get_data {
my ($cols, $rows) = @_;
my ($line, @rows);
my $i;
for ($i = 1; $i <= $rows and $line = <DATA>; $i++) {
chomp $line;
my $cells = [ split ' ', $line ];
die "Row $i had ", scalar(@$cells), " instead of $cols" if @$cells != $cols;
push @rows, $cells;
}
die "Not enough rows, got ", $i - 1, "\n" if $i != $rows + 1;
\@rows;
}
sub print_data {
my ($cols, $rows, $data) = @_;
for (my $i = 0; $i < $rows; $i++) {
for (my $j = 0; $j < $cols; $j++) {
say $data->[$i][$j];
}
}
}
my $data = get_data(6, 6);
print_data(6, 6, $data);
__DATA__
1 2 3 4 5 6
a b c d e f
6 5 4 3 2 1
f e d c b a
A B C D E F
7 8 9 10 11 12
Explanation:
say
, that avoids unsightly print ..., "\n"
get_data
is a function that can be called and/or reused, instead of just being part of the main scriptget_data
knows what data-shape it expects and throws an error if it doesn't get it[ ... ]
creates an anonymous array and returns a reference to itget_data
returns an array-reference so data isn't copiedprint_data
is a function toofor
loop instead of making lists of numbers, which in Perl 5 needs to allocate memoryThere is also a two-line version of the program (with surrounding bits, and test data):
use 5.10.0; # so can use "say"
my @lines = map { [ split ' ', <DATA> ] } (1..6);
map { say join ' ', map qq{"$_"}, @$_ } @lines;
__DATA__
1 2 3 4 5 6
a b c d e f
6 5 4 3 2 1
f e d c b a
A B C D E F
7 8 9 10 11 12
Explanation:
map
is the premier way to iterate over lists of things where you don't need to know how many you've seen (otherwise, a for
loop is needed)"
around the cell contents is only to prove they've been processed. Otherwise the second line could just be: map { say join ' ', @$_ } @lines;
Upvotes: 0
Reputation: 62236
i
and j
are not the same as the variables you declared in the foreach
lines. Change:
print $arr[i][j] . "\n";
to:
print $arr[$i][$j] . "\n";
warnings
alerted me to this issue. You should add these lines to all your Perl code:
use warnings;
use strict;
Upvotes: 4