我想在perl中使用函数创建哈希。其实我正在研究在perl中创建一个二叉搜索树。下面是代码:使用函数创建哈希
sub newhash {
$data = shift;
$left = undef;
$right = undef;
%node = ("data"=>$data,"left"=>$left,"right"=>$right);
return (\%node);
}
$firstele = newhash(2);
foreach (keys %$firstele)
{
print "$_:$firstele->{$_}\n";
}
$node = newhash(1);
foreach (keys %$node)
{
print "$_:$node->{$_} \n";
}
foreach (keys %$firstele)
{
print "$_:$firstele->{$_}\n";
}
麻烦的是,当我打印的原始哈希值,数据键得到由不管我是谁传递给newhash
功能所取代。输出:
left:
right:
data:2
left:
right:
data:1
left:
right:
data:1
任何想法为什么数据密钥被替换?
务必使用'use strict;使用警告;'! – ikegami 2014-09-29 05:18:04