我一直在玩 Perl 中的哈希。以下按预期工作:
use strict;
use warnings;
sub cat {
my $statsRef = shift;
my %stats = %$statsRef;
print $stats{"dd"};
$stats{"dd"} = "DDD\n";
print $stats{"dd"};
return ("dd",%stats);
}
my %test;
$test{"dd"} = "OMG OMG\n";
my ($testStr,%output) = cat (\%test);
print $test{"dd"};
print "RETURN IS ".$output{"dd"} . " ORIG IS ". $test{"dd"};
输出是:
OMG OMG
DDD
OMG OMG
RETURN IS DDD
ORIG IS OMG OMG
当我将一个数组添加到混合中时,它会出错。
use strict;
use warnings; sub cat {
my $statsRef = shift;
my %stats = %$statsRef;
print $stats{"dd"};
$stats{"dd"} = "DDD\n";
print $stats{"dd"}; return ("dd",("AAA","AAA"),%stats); }
my %test; $test{"dd"} = "OMG OMG\n";
my ($testStr,@testArr,%output) = cat (\%test);
print $test{"dd"};
print "RETURN IS ".$output{"dd"} . " ORIG IS ". $test{"dd"}. " TESTARR IS ". $testArr[0];
输出是:
OMG OMG
DDD
OMG OMG
Use of uninitialized value in concatenation (.) or string at omg.pl line 20.
RETURN IS ORIG IS OMG OMG
TESTARR IS AAA
为什么显示数组但不显示哈希?