我一直玩Perl的哈希。如預期了以下工作:如何從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
爲什麼陣列顯示,但散列是不是?
+1引用是絕對可用,可能是乾淨的,但有時他們_do_混淆 - 特別是初學者。 ) – raina77ow