【发布时间】:2013-02-02 00:20:34
【问题描述】:
我有一个 setter 子 setAssignmentStatus,它接受一个哈希数组(AoH 从这里开始)和另一个参数(不要关心这个,因为这部分有效),并做一些迭代通过 AoH 在每个散列元素中设置另一个条目。它不返回任何内容,因为我想在通过 setter sub 拉出后使用相同的 AoH 对象和添加的条目,而不是构造一个全新的 AoH 并重新填充条目。这是二传手:
sub setAssignmentStatus
{
my $fileFlatArySclr = $_[0];
my $cfgFile = $_[1];
#here I convert the AoH from the scalar necessary for the sub to its native form
my @fileFlatAry = @$fileFlatArySclr;
#this works, don't worry
my %cfgVarHash = getConfigVars($cfgFile);
foreach my $fileVarHashSclr(@fileFlatAry)
{
#convert each AoH entry from scalar necessary for iteration to native hash
my %varHash = %$fileVarHashSclr;
my $varName = $varHash{'VAR_NAME'};
my $asgnLineCnt = $varHash{'ASGN_CNT'};
my $asgnSts;
my $fileAsgnSts;
my $cfgAsgnSts;
if($asgnLineCnt > 0) { $fileAsgnSts = 1; } else { $fileAsgnSts = 0; }
my $cfgAsgnLine = $cfgVarHash{$varName};
if($cfgAsgnLine ne undef) { $cfgAsgnSts = 1; } else { $cfgAsgnSts = 0; }
$asgnSts = $fileAsgnSts.$cfgAsgnSts;
#debug to make sure $asgnSts is not null in the first place (it is not!)
print "\n*** setting ASGN_STUS of ".$varName." to ".$asgnSts;
#Here we set ASGN_STUS for every iteration
$varHash{'ASGN_STUS'} = $asgnSts;
}
}
如下调用:
setAssignmentStatus(\@fileFlatAry, $cfgFile);
但是,在通过setAssignmentStatus 发送@fileFlatAry AoH 之后,每个元素哈希不包含ASGN_STUS 条目。为什么会这样,我该如何解决?
我怀疑我对 \ 修饰符做错了,这就是我如何将数据结构作为标量参数传递给 sub 但我不确定。
【问题讨论】:
标签: perl data-structures pass-by-reference