我在從這個Perl代碼一些奇怪的結果 - 我需要刪除關聯對象的列表幾個要素。從Perl列表中刪除元素的安全方法?
我的方法是掃描列表一次推的比賽另一個數組,然後遍歷數組並刪除每一個,但我還沒有逃脫「而迭代陷阱不要刪除」。
有關如何避免這種情況的任何想法?非常感謝。
my @agentConfAssociationDeletionsList = (
"AcceptTPCookie",
"AgentNamesAreFQHostNames",
"BadCssChars",
"LogLocalTime"
);
#find associations to remove
my @associationsToRemove =();
foreach my $association ($agentConf->GetAssociations()) {
if (grep {$_ eq $association->Name()} @agentConfAssociationDeletionsList) {
print "pushing " . $association->Name() . "\n";
push(@associationsToRemove, $association);
}
}
#remove them
foreach my $association (@associationsToRemove) {
print "removing association: " . $association->Name();
agentConf->RemoveAssociation($association);
}
-1:那就是他已經在做:製作包含要被刪除原始列表項第二個列表,然後遍歷副本。 – 2010-11-01 07:12:34