【发布时间】:2017-02-12 22:17:08
【问题描述】:
我是 perl 的新手。这是我的第二个任务,我应该创建程序来解析 n 个文件并使用 n-grams 模型打印 m 个句子。长话短说,我编写了这个脚本,它将接受 n 个参数,其中第一个和第二个参数是数字,但其余的是文件名,但是我收到此错误 Wide character in print at ngram.pl line 35, line 1.
重现它的步骤:
命令行输入:perl ngram.pl 5 10 tale-cities.txt bleak-house.txt paper.txt
输出:在 ngram.pl 第 35 行第 1 行打印宽字符。
#!/usr/bin/perl
use strict;
use warnings FATAL => 'all';
use Scalar::Util qw(looks_like_number);
use utf8;
use Encode;
#Charles Dickens
sub checkIfNumberic
{
my ($inp)=@_;
if (looks_like_number($inp)){
return "True";
}
else{
return "False" ;
}
}
sub main
{
my $correctInput=", your input must be something like this 5 10 somefile.txt somefile2.txt ";
my @inputs= @ARGV;
if (checkIfNumberic($inputs[0]) eq "False"){
die "first argument must be numberic $correctInput\n";
}
if (checkIfNumberic($inputs[1]) eq "False"){
die "second argument must be numberic $correctInput\n";
}
for (my $i=2; $i< scalar @inputs ;$i++)
{
if (open(my $fh, '<:encoding(UTF-8)', $inputs[$i])) {
while (my $line = <$fh>) {
chomp $line;
print "$line \n";
}
}
}
}
main();
【问题讨论】:
-
提示:使用
my ($min, $max, @files) = @ARGV; -
提示:不要返回
True和False(它们都是 true),返回一些 true(通常是1)和一些 false(通常是0),然后执行 @ 987654327@,或只是if (looks_like_number($min)){ ... }
标签: perl