Why isn't a 2D array printed in Perl printing?
Handling 2D arrays in Perl is giving me a headache. Anyway, next question:
I have a loop that pushes an array like @twoOneArray into another array, say @twoDimArray, and then reset to the next iteration of the loop, and then reinserted into @twoDimArray with a new set of values, When I print this @twoDimArray using :
print Dumper \@twoDimArray;
he outputs
OUTPUT
$VAR1 = [
[
'BB',
'AA',
'AA'
],
$VAR1->[0],
$VAR1->[0],
$VAR1->[0]
];
or using loops
for (my $i=0; $i<4; $i++){
for (my $j=0; $j<4; $j++){
print "$twoDimArray[$i][$j] \n";
}
}
data is duplicated.
OUTPUT
Row = 0 BB AA AA
Row = 1 BB AA AA
Row = 2 BB AA AA
Row = 3 BB AA AA
etc....
I can't figure out why both exit paths go wrong. If I print @twoDimArray every time (before going to the next iteration of the loop, i.e. after using the push function) the @twoOneArray is inserted, then the values โโseem to be accurate and not repeated, but printing in one pass seems to give above mistake. A similar question was asked here , but I'm not sure if this makes sense to me. Any suggestions?
Code to build 2D array:
for ($k = 1; $k <= $counter; $k++){
@twoOneArray = (); #reset it when loop starts again
for ($j = 0; $j <= $colsInArray; $j++){
#do stuff to create @twoOneDim
}
push @twoDimArray, \@twoOneArray;
#if I print @twoDimArray if prints fine, with the exact values intact
}
print Dumper \@twoDimArray; #if I print it here it messes up
print "\n";
source to share
The Data :: Dumper output tells me that your problem isn't printing the array. Data: Damper never lies (or only rarely).
Please show us the code that you used to build the array. I'm sure there is a bug in this code somewhere.
UPDATE:
Now that you've added the code that creates the array, I can see that you've fallen into an evil trap: you're adding a @twoOneArray reference to your "outer" array. But the link will always be the same every time through your loop. Change your code like this:
for ($k = 1; $k <= $counter; $k++){
my @twoOneArray; # REALLY reset it when loop starts again
for ($j = 0; $j <= $colsInArray; $j++){
#do stuff to create @twoOneDim
}
push @twoDimArray, \@twoOneArray;
}
.. and it should work.
source to share
You did something like this:
@a = (1, 2, 3);
push @b, \@a;
@a = (2, 3, 4);
push @b, \@a;
# ...
The problem is that you click @b
on the link to @a
. All links point to the same @a
. Data::Dumper
tells you what's $VAR->[0]
wrong with his thing.
You need to do something like:
$a = [ 1, 2, 3 ];
push @b, $a;
$a = [ 2, 3, 4] ;
push @b, $a;
# ....
So the [ ... ]
arrayref syntax will create a new reference array every time.
I suggest you read perlreftut and perlref carefully.
edit: I see that you posted your code and, yes, you do my code with code. Change your syntax usage [ ... ]
and you should be fine.
source to share
Your question has been satisfied by others. I would add that the inference from Data::Dumper
is useful in identifying problems like this: [1]
your array element is being dumped as $VAR1->[0]
- in other words, it is a reference to the same underlying data values โโstored in [0]
.
source to share