如果不查看输入文件的格式,就很难建议您如何将其分成五个块,但这里有一些您可以使用的通用指针。
读取文件,一次一行
如果您的输入文件每行有一个问题,您可以使用与您在脚本中发布的内容类似的方法阅读它。
test.txt 的内容:
Q1. Why colour is red?
Q2. How many is too many?
Q3. What about me?
脚本:
use strict;
use warnings;
use Data::Dumper; # to visualise data structures
my $filename = "test.txt";
open my $test, '<', $filename
or die "Couldn't open file '$filename' $!\n";
my @lines;
# go through text.txt one line at a time
while (<$test>) {
chomp; # remove the line ending
push @lines, $_; # push each line into the array "lines"
}
# view the data structure
print Dumper \@lines;
输出:
$VAR1 = [
'Q1. Why colour is red?',
'Q2. How many is too many?',
'Q3. What about me?'
];
执行此操作的简写方式是:
open my $test, '<', $filename
or die "Couldn't open file '$filename' $!\n";
my @lines = <$test>; # read all lines into @lines
chomp @lines; # remove all the line endings
print Dumper \@lines;
输出:
$VAR1 = [
'Q1. Why colour is red?',
'Q2. How many is too many?',
'Q3. What about me?'
];
现在您的所有问题都在数组@lines 中;第 1 题是 $lines[0],第 2 题是 $lines[1],第 3 题是 $lines[2],以此类推。
分块读取文件
如果您的输入文件有一个问题,然后是三个可能的答案(如您之前的问题),那该怎么办?输入文件格式示例:
1.) question one text
a.) solution a
b.) solution b
c.) solution c
2.) question two text
a.) solution 2a
b.) solution 2b
c.) solution 2c
3.) question three text
a.) solution 3a
b.) solution 3b
c.) solution 3c
您可以将input record separator, $/ 设置为自定义值,以将您的文件拆分为您认为的记录。默认情况下,$/ 将一行视为一条记录。如果将$/ 设置为"",它将以段落模式读取文件,这意味着任何两个或多个连续新行(即\n\n、\n\n\n 等)的集合都将分隔记录。要解析上述格式的文件,您可以:
my $filename = "test.txt";
my @lines;
{
local $/ = "";
open my $test, '<', $filename
or die "Couldn't open file '$filename' $!\n";
@lines = <$test>;
}
print Dumper \@lines;
输出:
$VAR1 = [
'1.) question one text
a.) solution a
b.) solution b
c.) solution c
',
'2.) question two text
a.) solution 2a
b.) solution 2b
c.) solution 2c
',
'3.) question three text
a.) solution 3a
b.) solution 3b
c.) solution 3c
'
];
如果我们想对输入进行任何操作,给输入记录分隔符一个不同的值是很有用的。例如,让我们将每个问题分成一组行:
my $filename = "test.txt";
my @lines;
my @questions;
{
local $/ = "";
open my $test, '<', $filename
or die "Couldn't open file '$filename' $!\n";
while (<$>) {
# split up the input into lines
my @temp = split "\n", $_;
# find lines starting with a digit and a full stop ( /^\d\./ ) from @temp
# push them on to the array @questions
push @questions, grep /^\d+\./, @temp;
# push a reference to @temp on to @lines (@lines is now an array of arrays)
push @lines, [ @temp ];
}
}
print Dumper \@questions;
输出:
$VAR1 = [
'1.) question one text',
'2.) question two text',
'3.) question three text'
];
查看@lines的结构:
print Dumper \@lines;
输出:
$VAR1 = [
[
'1.) question one text',
' a.) solution a',
' b.) solution b',
' c.) solution c'
],
[
'2.) question two text',
' a.) solution 2a',
' b.) solution 2b',
' c.) solution 2c'
],
[
'3.) question three text',
' a.) solution 3a',
' b.) solution 3b',
' c.) solution 3c'
]
];
以单个字符串形式读取文件
另一种处理文件内容的常用方法是将文件作为一个长字符串读取。为此,您需要将输入记录分隔符设置为未定义:
test.txt 的内容:
Q1. Why colour is red?
Q2. How many is too many?
Q3. What about me?
脚本:
my $filename = "test.txt";
my $lines;
{
local $/;
open my $test, '<', $filename
or die "Couldn't open file '$filename' $!\n";
$lines = <$test>;
}
print Dumper($lines);
输出:
$VAR1 = 'Q1. Why colour is red?
Q2. How many is too many?
Q3. What about me?
';
您应该能够在这些方法中找到至少一种适合您的输入和您的目的的方法。如果不能,您需要发布您的输入文件以及您希望输出的样子。
More information on file operations in perlfaq5
More information on Perl's special variables in perlvar