title: Why isn't the two dimensional array in Perl printing correctly?
body: <p>Handling two dimensional arrays in Perl is giving me a headache. Anyway, the following is my question:</p>
  
  <p>I have a loop which pushes an array, say @twoOneArray, into another array, say @twoDimArray, and then is reset before the next iteration of loop begins and then again is pushed into @twoDimArray with new set of values. When I print this @twoDimArray using either: </p>
  
  <pre><code>print Dumper \@twoDimArray;
  </code></pre>
  
  <p>it gives output </p>
  
  <p><em>OUTPUT</em></p>
  
  <pre><code>$VAR1 = [
  
        [
  
          'BB',
  
          'AA',
  
          'AA'
         ],
         $VAR1-&gt;[0],
         $VAR1-&gt;[0],
         $VAR1-&gt;[0]
       ];
  </code></pre>
  
  <p>or using loops </p>
  
  <pre><code>for (my $i=0; $i&lt;4; $i++){
      for (my $j=0; $j&lt;4; $j++){
           print "$twoDimArray[$i][$j] \n";
      }
  }
  </code></pre>
  
  <p>the data gets duplicated. </p>
  
  <p><em>OUTPUT</em></p>
  
  <p>Row=0   BB AA AA </p>
  
  <p>Row=1   BB AA AA</p>
  
  <p>Row=2   BB AA AA </p>
  
  <p>Row=3   BB AA AA </p>
  
  <p>and so on....</p>
  
  <p>I cant figure out why the both the output ways are going wrong. If I print @twoDimArray everytime (before moving to next iteration of loop, i.e. after using push function) @twoOneArray is inserted, then the values seem to be fine and not repeat themselves, but printing it in a single go seems to give the above error. Similar question has been asked <a href="https://stackoverflow.com/questions/1112988/how-can-i-print-a-two-dimensional-array-in-perl">here</a> but I am not sure if it makes sense to me. Any suggestions?</p>
  
  <p>Code to build 2D array:</p>
  
  <pre><code>for ($k = 1; $k &lt;= $counter; $k++){
          @twoOneArray = (); #reset it when loop starts again
          for ($j = 0; $j &lt;= $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";
  </code></pre>