-2
我正在執行大型(8000行)庫數據轉換。我在一個文件中讀取並且想要逐行修改它。在到達文件結尾之前停止讀取文件。我的腳本在讀取文件時未命中行
open(my $infh, "<", 'infile.pica')
|| die('Could not open pica file');
open(my $outfh, ">", 'infile.pica.norm')
|| die('Could not open pica file');
my $counter = 0;
while (my $line = <$infh>) {
$counter++;
# for debugging - this is the last line being read.
# infile actually has 7857 lines
if ($counter >= 7691) {
say $line;
}
# modification commented out for debugging
print $outfh $line;
}
close $infh;
close $outfh;
我首先想到的是,有在這一行一個奇怪的字符,但沒有什麼
006X $cEBC$03564211 (original)
006X $cEBC$035642 (being read, thats what the say prints)
這是該數據集的片段,其中停止閱讀:
[email protected] $0Oax
002C $aText$btxt
002D $aComputermedien$bc
002E $aOnline-Ressource$bcr
004A $09780309160193
006X $cEBC$03564211
[email protected] $aeng
[email protected] $a2010
您可以看到每行後面都有一個換行符 (嗨代碼0A
)。 006X行是停止閱讀的地方。
你的代碼看起來不錯,可能會發布一些你的設置的更多細節? –
你看過線上的最後一個字符嗎?默認情況下,Perl會在換行符('\ n')上拆分行 - 所有行都有一行嗎? – reinierpost
你可以用記事本++這樣的東西打開文件,讓它顯示所有的字符(如CR,LF等),然後看看嫌疑人的行,可能確實有些東西你不能「看」 – Nullman