我在尋求一些有關此代碼效率的反饋,以及如何從@FilesToUse數組中移除符合條件的文件。用perl檢查文本文件的內容
我將一個目錄中的所有文件加載到@FilesToUse數組中。然後我需要檢查每個文件,看看第7行是否顯示'BATCH',如果它是我想移動文件,然後從@FilesToUse數組中刪除文件名,所以當我稍後使用該數組時沒有BATCH文件名在裏面。我希望我明確提出這個問題。
文本文件大約是10Mb,我通常每天要處理2個文件。
在此先感謝您的幫助。
#Loop thru the files that were found and delete the one that is BATCH
foreach $FileToUse (@FilesToUse) #Loop at the file level
{
open (FH, $TheInputDir . $FileToUse) or $MailMsg = $MailMsg . "ERROR: Could not open input file: $FileToUse \n";
while (<FH>) # Loop at the line level within each file
{
$TheLine = $_;
chomp($TheLine);
$LineCnt++;
if ($LineCnt == 7 and substr($TheLine, 1, 6) =~ /BATCH/) {
move("$TheInputDir$FileToUse", "$TheBatchMoveDir")
or $MoveFail = 1;
if ($MoveFail == 1) {
$MailMsg = $MailMsg
. "ERROR: Failed to move $FileToUse to Batch folder!\n";
}
else {
$MailMsg
= $MailMsg . "Moved BATCH file $FileToUse to Batch folder\n";
}
}
last if $. == 7;
}
}
輸入文件的示例也很有用。 –
而不是刪除一個元素,如果它從來沒有被添加到「@ FilesToUse」中,它會不會更好? –
另外,你可以爲這個問題創建一個[mcve]嗎? –