[英]perl syntax error at wordlist.pl line 11, near “) $fh”
my $fn= "words.txt";
open ($fn), $file;
if (! -e "$fh") $fh="STDIN";
while (<$fn>){;
my $total_words = @words; #All word count
my %count;
$count{$_}++ for @words; # Here are the counts
my $uniq_words = scalar keys %count; # Number of uniq words
}
# Print sorted by frequency
print "$_\t$count{$_}" for (sort { $count{$b} <=> $count{$a} } keys %count);
close FILE;
exit 0
I'm getting this error: 我收到此错误:
Scalar found where operator expected at wordlist.pl line 8, near ") $fh"
(Missing operator before $fh?)
syntax error at wordlist.pl line 8, near ") $fh"
Execution of wordlist.pl aborted due to compilation errors.
please help 请帮忙
Perl always requires braces around the code after a conditional: 有条件的话,Perl 总是要求在代码两边加上大括号:
You wrote: 你写了:
if (! -e "$fh") $fh="STDIN";
You should write either: 您应该写:
if (! -e "$fh") { $fh="STDIN"; }
Or: 要么:
$fh = "STDIN" if ! -e "$fh";
Those are syntactically correct. 这些在语法上是正确的。 The code is semantically shot to pieces, though.
不过,该代码在语义上是一shot而就的。 To open a file, use:
要打开文件,请使用:
open my $fh, '<', $fn or die "Failed to open $fn";
And always use use strict;
并始终
use strict;
and use warnings;
并
use warnings;
. 。 Perl experts use them to ensure they've not made silly mistakes.
Perl专家使用它们来确保他们没有犯傻的错误。 Novices should do the same.
新手也应该这样做。
open ($fn), $file;
Remove the parentheses here. 在此处删除括号。
The first argument is the filehandle, and the second is the filename. 第一个参数是文件句柄,第二个参数是文件名。 You're using
$fn
as both the filename and filehandle, whereas $file
is never defined. 您将
$fn
用作文件名和文件句柄,而从未定义$file
。
if (! -e "$fh") $fh="STDIN";
You can't leave off the braces around a block like this. 您不能在这样的块周围留下括号。 I'm also not sure what
$fh
is supposed to be since you never use it again. 我也不确定
$fh
应该是什么,因为您再也不会使用它了。
You seem confused about Perl's syntax. 您似乎对Perl的语法感到困惑。 How are you learning Perl?
您如何学习Perl?
Probably you got confused between file name $file
and file handle $fh
可能您在文件名
$file
和文件句柄$fh
之间感到困惑
Try changing your first three lines to 尝试将您的前三行更改为
my $file= "words.txt";
if (! -e $file) {
my $fh = *STDIN;
} else {
open my $fh, '<', $file;
}
And it seems there is typo in $fn
. $fn
似乎有错字。 Shouldn't it be $fh
? 不是
$fh
吗?
Your understanding of open
is incorrect. 您对
open
理解不正确。 The modern way of using it with a three argument call is typically: 通过三个参数调用使用它的现代方式通常是:
open my $fh, '<', <file name> or die $!;
You specify a new file handle object as the first argument, not the file name. 您指定一个新的文件句柄对象作为第一个参数,而不是文件名。 You also do not need to open a file to check if it exists.
您也不需要打开文件来检查它是否存在。 So instead of doing that just do something along the lines of:
因此,除了执行以下操作之外,还可以执行以下操作:
my $file = 'words.txt';
if (! -e $file) {
print "$file does not exist\n";
}
else {
# open your file here and remember to close it
}
Then just use the special diamond operator <>
to operate on STDIN
if it does not exist. 如果不存在,请使用特殊的菱形运算符
<>
在STDIN
上进行操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.