title: understanding data structures in perl
body: <p>I'm trying to understand the 'Common Mistake' section in the perldsc documentation. What is the author trying to convey when he mentions:</p>
  
  <blockquote>
    <p>The two most common mistakes made in constructing something like an array of arrays is either accidentally counting the number of elements or else taking a reference to the same memory location repeatedly. Here's the case where you just get the count instead of a nested array:</p>
  </blockquote>
  
  <pre><code>for my $i (1..10) {
      my @array = somefunc($i);
      $AoA[$i] = @array;      # WRONG!
  }
  </code></pre>
  
  <p>From what I understand is that when it iterate it will take the first value of (1..10) which is 1 and will pass it to a function like this:</p>
  
  <pre><code>my @array = somefunc(1);
  </code></pre>
  
  <p>Since that function is not defined, I'll create the logic.</p>
  
  <pre><code>sub somefunc  {
  my $a = shift;
  print $a * $a;
  }
  </code></pre>
  
  <p>which will essentially do this:</p>
  
  <pre><code>1 * 1
  </code></pre>
  
  <p>and the result is '1'.</p>
  
  <p>To my understanding   my @array will look like:</p>
  
  <pre><code>@array = ('1');
  </code></pre>
  
  <p>And the next line will do:</p>
  
  <pre><code>$AoA[$i] = @array;
  </code></pre>
  
  <p>I'm assuming that $AoA[$1] is an anonymous array ( he/she didn't declare with 'my', btw) and the @array will be the first element of the this anonymous array which the author said it' WRONG. And the for each loop with iterate to '2'.</p>
  
  <pre><code>somefunc(2);
  </code></pre>
  
  <p>Which will be '4' and passed to:</p>
  
  <pre><code>$AoA[$i] = @array
  </code></pre>
  
  <p>What is the point of the author with this code which is wrong. I'm trying to understand what is wrong but more importantly, I'm trying to understand the code. Any help will be appreciated.</p>
  
  <p>UPDATE</p>
  
  <p>I think I understand why this is a common mistake because when I use print and Dumper, I can visually see what the author is trying to convey, here is the revised code.</p>
  
  <pre><code>#!/usr/bin/perl -w
  use strict;
  use Data::Dumper;
  
  for my $i (1..10) {
             my @AoA;
             my @array = somefunc($i);
             print "The array is Dumper(@array)\n";
             $AoA[$i] = @array;      # WRONG!
             print Dumper($AoA[$i]);
         }
  
  
  sub somefunc  {
  my $a = shift;
  return $a * $a;
  }
  </code></pre>
  
  <p>In the Common Mistakes paragraph of perldoc perldsc, he/she states</p>
  
  <blockquote>
    <p>Here's the case where you just get the count instead of a nested array:</p>
  </blockquote>
  
  <p>Below is the output of the Dumper.</p>
  
  <pre><code>The array is Dumper(1)
  $VAR1 = 1;
  The array is Dumper(4)
  $VAR1 = 1;
  The array is Dumper(9)
  $VAR1 = 1;
  The array is Dumper(16)
  $VAR1 = 1;
  The array is Dumper(25)
  $VAR1 = 1;
  The array is Dumper(36)
  $VAR1 = 1;
  The array is Dumper(49)
  $VAR1 = 1;
  The array is Dumper(64)
  $VAR1 = 1;
  The array is Dumper(81)
  $VAR1 = 1;
  The array is Dumper(100)
  $VAR1 = 1;
  </code></pre>
  
  <p>So I'm assuming that the repeated</p>
  
  <pre><code>$VAR1 = 1;
  </code></pre>
  
  <p>is the count and not the nested array.</p>
  
  <p>The author did indicate that if the count is what I truly want then to rewrite the code like this:</p>
  
  <pre><code>#!/usr/bin/perl -w
  use strict;
  use Data::Dumper;
  
  for my $i (1..10) {
             my @count;
             my @array = somefunc($i);
             print "The array is Dumper(@array)\n";
             $count[$i] = scalar @array;      
             print Dumper($count[$i]);
         }
  
  
  sub somefunc  {
  my $a = shift;
  return $a * $a;
  }
  </code></pre>
  
  <p>But the documentation didn't tell me how to get the nested array?</p>
  
  <p>UPDATE</p>
  
  <p>Correct me if I am wrong but I rewrote the code to get the nested array:</p>
  
  <pre><code>#!/usr/bin/perl -w
  use strict;
  use Data::Dumper;
  my @count;
  my @new_array;
  
  for my $i (1..10) {
             #my @count;
             my @array = somefunc($i);
             push @new_array, [@array];
         }
  
  
  sub somefunc  {
  my $a = shift;
  return $a * $a;
  }
  
  print Dumper(\@new_array);
  </code></pre>
  
  <p>Which printed</p>
  
  <pre><code>$VAR1 = [
        [
          1
        ],
        [
          4
        ],
        [
          9
        ],
        [
          16
        ],
        [
          25
        ],
        [
          36
        ],
        [
          49
        ],
        [
          64
        ],
        [
          81
        ],
        [
          100
        ]
      ];
  </code></pre>