我有一個Perl腳本,它將包含多個句子(Sentences.txt
)的文本文件作爲輸入。每個句子用白線分隔。該腳本爲Sentences.txt
中的每個句子創建單獨的文本文件。例如,Sent1.txt
爲第一句Sentences.txt
,Sent2.txt
爲第二句Sentences.txt
等。在perl中使用printf函數打印%字符
問題是當我嘗試使用printf
功能從Sentences.txt
打印一個句子到相應的單獨的文件(SentX.txt
)和句子包含%
字符。我該如何解決這個問題?
這是代碼:
#!/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";
你確定你需要在這裏使用'printf'嗎?也許使用'print'? – Suic