我有一个setter sub 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);
但是,在通过@fileFlatAry
发送setAssignmentStatus
AoH后,每个元素哈希都不包含ASGN_STUS条目。为什么这样,我该如何解决?
我怀疑我对\
修饰符做错了,这就是我如何将数据结构作为标量参数传递给sub,但我不确定。
答案 0 :(得分:3)
您修改%varHash
而不是修改引用的哈希。停止将所有内容复制到局部变量中并修改局部变量。
$varHash{'ASGN_STUS'} = ...;
应该是
$fileVarHashSclr->{'ASGN_STUS'} = ...;
我也不会my @fileFlatAry = @$fileFlatArySclr;
。纯粹的浪费。