哈希中的意外值?

Unexpected values in hash?

我正在尝试创建一个迷宫生成器,但我的代码遇到了一些问题。在脚本的开头,我创建了一个包含 X 列和 Y 行的多维数组 (@maze)。有一个循环遍历数组并将所有元素设置为初始值 1。get_neighbors 子例程假设创建一个具有 toprightbottom, left 键。然后它根据传入的 xy 坐标设置这些键的值。对于 top 键,我将它的值设置为正上方的元素应该是 [ $y - 1 ][ $x ]。例如,我假设如果传入坐标 0, 0top 将设置为 undef,因为那不是有效的元素/位置,但它不是……它被设置为 1。不知道为什么……希望有人能发现并解释为什么会这样。这是整个脚本:

#!/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 < $rows; ++$y) {
    for(my $x = 0; $x < $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 < $rows; ++$y) {
        for(my $x = 0; $x < $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;
}

我运行这个脚本出来的时候是:

# 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 

在 Perl 中,数组的负数下标从数组末尾算起。

例如:

my @foo = ('a', 'b', 'c', 'd', 'e');

say $foo[-2];

会显示d.

这应该说明发生了什么:

use warnings;
use strict;
use feature 'say';

my @maze = ( [ 'a' .. 'e' ],
             [ 'f' .. 'j' ],
             [ 'k' .. 'o' ],
             [ 'p' .. 't' ],
             [ 'u' .. 'y' ], );

for my $row (@maze) {
    say "@$row";
}

my %neighbors;
my ($x, $y) = (0, 0);

# The // defined-or was added in Perl 5.10.  These are equivalent:
# $foo = defined($bar) ? $bar : 'toast';
# $foo = $bar // 'toast';

$neighbors{'top'}    = $maze[ $y - 1 ][ $x ] // '-'; 
$neighbors{'bottom'} = $maze[ $y + 1 ][ $x ] // '-';
$neighbors{'left'}   = $maze[ $y ][ $x - 1 ] // '-';
$neighbors{'right'}  = $maze[ $y ][ $x + 1 ] // '-';

say "  $neighbors{top}";
say "$neighbors{left} $maze[$y][$x] $neighbors{right}";
say "  $neighbors{bottom}";

输出:

a b c d e
f g h i j
k l m n o
p q r s t
u v w x y
  u
e a b
  f

尝试:

$neighbors{ top    } = $y > 0              ? $maze[ $y - 1 ][ $x ] : undef;
$neighbors{ bottom } = $y < $#maze         ? $maze[ $y + 1 ][ $x ] : undef;
$neighbors{ left   } = $x > 0              ? $maze[ $y ][ $x - 1 ] : undef;
$neighbors{ right  } = $x < $#{ $maze[0] } ? $maze[ $y ][ $x + 1 ] : undef;