正如标题所说,perl 在查询到不存在的元素后将虚拟元素添加到数组中。查询后数组大小增加。行为说明:
my $rarr;
$rarr->[0][0] = 'S';
$rarr->[0][1] = 'MD';
$rarr->[1][0] = 'S';
$rarr->[1][1] = 'PRP';
my $crulesref;
$crulesref->[0] = $rarr;
check_rule('aa', 0);
if($rarr->[3][0] == 'M'){ # just check a not existing element
print "m\n";
}
check_rule('bb', 0);
if($rarr->[5][0] == 'M'){ # again: just check a not existing element
print "m\n";
}
check_rule('cc', 0);
sub check_rule($$)
{
my ($strg,$ix) = @_;
my $aref = $crulesref->[$ix];
my $rule_size = @$aref;
{print "-----$strg aref:$aref rs:$rule_size aref:'@$aref'\n";
for(my $t1 = 0; $t1 <$rule_size; $t1++){
print "t1:$t1 0:$aref->[$t1][0] 1:$aref->[$t1][1]\n";
}
}
}
运行的结果是:
en@en-desktop ~/dtest/perl/forditas/utf8_v1/forditas/test1 $ perl v15.pl
-----aa aref:ARRAY(0x90ed8c8) rs:2 aref:'ARRAY(0x9106cac) ARRAY(0x9106d24)'
t1:0 0:S 1:MD
t1:1 0:S 1:PRP
m <-------------- finds the non existing
-----bb aref:ARRAY(0x90ed8c8) rs:4 aref:'ARRAY(0x9106cac) ARRAY(0x9106d24) ARRAY(0x9107508)'
t1:0 0:S 1:MD
t1:1 0:S 1:PRP
t1:2 0: 1: <-- undesired dummy due to inquiry
t1:3 0: 1: <-- undesired dummy due to inquiry
m <-------------- finds the non existing
-----cc aref:ARRAY(0x90ed8c8) rs:6 aref:'ARRAY(0x9106cac) ARRAY(0x9106d24) ARRAY(0x9107904) ARRAY(0x9107508) ARRAY(0x910e860)'
t1:0 0:S 1:MD
t1:1 0:S 1:PRP
t1:2 0: 1: <-- undesired dummy due to inquiry
t1:3 0: 1: <-- undesired dummy due to inquiry
t1:4 0: 1: <-- undesired dummy due to inquiry
t1:5 0: 1: <-- undesired dummy due to inquiry
如果被询问的元素存在,除了在每次询问之前询问之外,没有其他方法可以避免这种情况吗?我尝试提高速度,但这些查询会减慢代码速度,并使其不易阅读。
提前感谢有用的提示。