我有一个 Perl 脚本,它将包含多个句子 ( ) 的文本文件作为输入Sentences.txt
。每个句子都用白线隔开。该脚本为Sentences.txt
. 例如,Sent1.txt
对于中的第一句Sentences.txt
,Sent2.txt
对于中的第二句Sentences.txt
,依此类推。
当我尝试使用该函数将句子从Sentences.txt
相应的单独文件 ( ) 打印并且该句子包含一个字符时,问题就出现了。我该如何解决这个问题?SentX.txt
printf
%
这是代码:
#!/usr/bin/perl -w
use strict;
use warnings;
# Separate sentences
my $sep_dir = "./sep_dir";
# Sentences.txt
my $sent = "Sentences.txt";
open my $fsent, "<", $sent or die "can not open '$sent'\n";
# read sentences
my $kont = 1;
my $previous2_line = "";
my $previous_line = "";
my $mom_line = "";
while(my $line = <$fsent>){
chomp($line);
#
$previous2_line = $previous_line;
#
$previous_line = $mom_line;
#
$mom_line = $line;
if($mom_line !~ m/^\s*$/){
# create separate sentence file
my $fitx_esal = "Sent.$kont.txt";
open my $fesal, ">", $fitx_esal or die "can not open '$fitx_esal'\n";
printf $fesal $mom_line;
close $fesal or die "can not close '$fitx_esal'.\n";
$kont++;
}
}
close $fsent or die "can not close '$sent'.\n";