title: Two-dimensional array access in Perl
body: <p>I have an array populated with cities. I want to pass the array by reference to a sub routine and print each city to output. However, I have the following problems:</p>
  <ol>
  <li><p>I can access each element before my <em>while</em> loop in the subroutine. But I cannot access the elements within my <em>while</em> loop. I get the error message:</p>
  <p>...
  Use of uninitialized value in print at  line 44,  line 997 (#1)
  Use of uninitialized value in print at  line 44,  line 998 (#1)
  ...</p>
  </li>
  </ol>
  <p>The following is some code. I have commented what prints and what doesn't (I tried to cut out code that is not needed for my explanation...):</p>
  <pre><code>@cities;
  
  # Assume cities is loaded successfully
  &amp;loadCities(getFileHandle('cities.txt'), $NUM_CITIES, \@cities);
  &amp;printElements(getFileHandle('names.txt'), \@cities);
  
  sub printElements{
  
      my $counter = 0;
      my $arraySize = scalar $_[1];
  
      # Prints fine!!!
      print @{$_[1][($counter)%$arraySize];
  
      while ((my $line = $_[0]-&gt;getline()) &amp;&amp; $counter &lt; 1000){
  
          # Doesn't print. Generates the above error
          print @{$_[1][($counter)%$arraySize];
  
          $counter += 1;
      }
  }
  </code></pre>
  <ol start="2">
  <li>The Perl syntax has me super confused. I do not understand what is going on with @{$_[1]}[0]. I am trying to work it out.</li>
  </ol>
  <ol>
  <li>$_[1], treat the value at this location as scalar value (memory
  address of the array)</li>
  <li>@{...}, interpret what is stored at this
  memory address as an array</li>
  <li>@{...} [x], access the element at index x</li>
  </ol>
  <p>Am I on the right track?</p>