title: Unexpected values in hash?
body: <p>I'm trying to create a maze generator and I'm having some trouble with my code. At the beginning of my script, I create a multidimensional array (<code>@maze</code>) with X number of columns and Y number of rows. There is a loop which iterates through the array and sets all of the elements to an initial value of 1. The <code>get_neighbors</code> sub routine is suppose to create a hash with <code>top</code>, <code>right</code>, <code>bottom</code>, <code>left</code> keys. It then sets the values for these keys based on the <code>x</code> and <code>y</code> coords which get passed in. For the <code>top</code> key, I set it's value to the element directly above which should be <code>[ $y - 1 ][ $x ]</code>. I was assuming that if for example, the coords, <code>0, 0</code> were passed in, <code>top</code> would be set to undef since that's not a valid element / position, but it's not.. It's being set to 1. Not sure why.. Was hoping someone could spot and explain why that's happening. Here's the entire script:</p>
  
  <pre><code>#!/usr/bin/perl
  
  use warnings;
  use strict;
  use Switch;
  use Data::Dumper;
  
  # Rows is set equal to the first arg passed in
  # unless it hasn't been. If it hasn't been, the
  # value defaults to 60. This also applies to the
  # columns.
  my $rows  = (defined($ARGV[ 0 ]) ? $ARGV[ 0 ] : 60);
  my $cols  = (defined($ARGV[ 1 ]) ? $ARGV[ 1 ] : 60);
  my $cells = $rows * $cols;
  
  my @maze;
  
  # "Pre-allocate" each "Cell" for the maze.
  for(my $y = 0; $y &lt; $rows; ++$y) {
      for(my $x = 0; $x &lt; $cols; ++$x) { $maze[ $y ][ $x ] = 1; }
  } 
  
  # Run 
  main();
  
  #--------------------------
  # Main ~
  #--------------------------
  sub main {
      print Dumper(get_neighbors(0, 0));
  
      generate();
      print_maze();   
  
      return;
  }
  
  #--------------------------
  # Generate the maze w/ BFS
  #--------------------------
  sub generate {
  
      return;
  }
  
  #--------------------------
  # Print maze to console
  #--------------------------
  sub print_maze {
      for(my $y = 0; $y &lt; $rows; ++$y) {
          for(my $x = 0; $x &lt; $cols; ++$x) {
              print $maze[ $y ][ $x ] . " ";
          }
          print "\n";
      }
  }
  
  #--------------------------
  # Returns the values of 
  # neighboring cells 
  #--------------------------
  sub get_neighbors {
      my $x = shift;
      my $y = shift;
  
      my %neighbors;
  
      $neighbors{'top'}    = defined($maze[ $y - 1 ][ $x ]) ? $maze[ $y - 1 ][ $x ] : undef; 
      $neighbors{'bottom'} = defined($maze[ $y + 1 ][ $x ]) ? $maze[ $y + 1 ][ $x ] : undef;
      $neighbors{'left'}   = defined($maze[ $y ][ $x - 1 ]) ? $maze[ $y ][ $x - 1 ] : undef;
      $neighbors{'right'}  = defined($maze[ $y ][ $x + 1 ]) ? $maze[ $y ][ $x + 1 ] : undef;
  
      return %neighbors;
  }
  </code></pre>
  
  <p>The out when I run this script is:</p>
  
  <pre><code># perl mazegen.pl 10 10
  $VAR1 = 'left';
  $VAR2 = 1;
  $VAR3 = 'right';
  $VAR4 = 1;
  $VAR5 = 'top';
  $VAR6 = 1;
  $VAR7 = 'bottom';
  $VAR8 = 1;
  1 1 1 1 1 1 1 1 1 1 
  1 1 1 1 1 1 1 1 1 1 
  1 1 1 1 1 1 1 1 1 1 
  1 1 1 1 1 1 1 1 1 1 
  1 1 1 1 1 1 1 1 1 1 
  1 1 1 1 1 1 1 1 1 1 
  1 1 1 1 1 1 1 1 1 1 
  1 1 1 1 1 1 1 1 1 1 
  1 1 1 1 1 1 1 1 1 1 
  1 1 1 1 1 1 1 1 1 1 
  </code></pre>