我正在尝试创建一个简单的脚本来读取包含书名记录的文本文件。每条记录都用一个普通的旧双空格 ( \r\n\r\n
) 分隔。我需要计算文件中有多少条记录。
例如这里是输入文件:
record 1
some text
record 2
some text
...
我正在使用正则表达式来检查回车符和换行符,但它不匹配。我究竟做错了什么?我无计可施。
sub readInputFile {
my $inputFile = $_[0]; #read first argument from the commandline as fileName
open INPUTFILE, "+<", $inputFile or die $!; #Open File
my $singleLine;
my @singleRecord;
my $recordCounter = 0;
while (<INPUTFILE>) { # loop through the input file line-by-line
$singleLine = $_;
push(@singleRecord, $singleLine); # start adding each line to a record array
if ($singleLine =~ m/\r\n/) { # check for carriage return and new line
$recordCounter += 1;
createHashTable(@singleRecord); # send record make a hash table
@singleRecord = (); # empty the current record to start a new record
}
}
print "total records : $recordCounter \n";
close(INPUTFILE);
}