1
我有以下代碼可以刪除我正在使用的文本文件中的行。但是,它實際上並沒有刪除我想要的行。我在這裏做了很多測試,並確認它會刪除行,如果我的行號是正確的。然而,$.
似乎是複製實際的行數(當我在上面打印,它說,有雙有行的數量實際上是!)perl Tie ::刪除文本文件中的行
#!/usr/bin/perl
# find_company_codes.pl
use strict;
use warnings;
use diagnostics;
use Tie::File;
my $split_file = "//server/folder/splits_table.txt";
# remove fields where only MAS has rollcall
my @file;
my @lines;
tie @file, 'Tie::File', $split_file or die $_;
foreach (@file) {
@lines = split(",",$_);
next if $lines[0] eq "ESDB Link Processing Reg-Prd-CCD";
next if $lines[2] ne "";
next if $lines[3] ne "";
next if $lines[4] ne "";
next if $lines[5] ne "";
next if $lines[6] ne "";
splice(@file, $.,1);
}
untie @file;
附: - 我知道我可以使用this,但是,我認爲在這種情況下使用'Tie :: File'會更直接一點。
感謝您的支持。我正在經歷你剛剛寫下的所有痛苦。我的代碼已經運行了** WAY **太長了。我擔心,並發現困難。再次感謝您的幫助和解釋。 – 2013-02-12 20:43:46
該代碼會將整個文件加載到內存中,但這樣做並不重要,因此它不會更改。 – ikegami 2013-02-12 21:01:21
我不認爲這實際上是通過文件的行。從我所看到的這個'my @fields = split(/,/,$ file [$ _]);'一直引用文件的第一行(標題行)。 – 2013-02-12 21:03:48