为什么我的程序使用 Perl 的 getc 函数无法正常工作?
我想使用 Perl 计算消息中字符出现的频率。例如,如果字符“a”在消息中出现 10 次,则频率将为 10。为此,我使用 getc 函数。这是我写的片段。我知道这是非常基本的。但是,当我编译时,出现错误:
详细信息:
#!/usr/bin/perl
use strict;
use warnings;
my $input=$ARGV[0];
open(INPUT,"<$input");
while(<INPUT>
{
my $c=getc(INPUT);
print $c."\n";
}
close(INPUT);
当我尝试编译它时,出现以下错误:
Use of uninitialized value in print at AccessChar.pl line 13, ;第 1 行。
我无法弄清楚这个脚本有什么问题。有人可以帮我解决这个问题吗?
我什至尝试使用 getc
INPUT 而不是 getc(INPUT)
。我认为运行此脚本时不需要包含任何其他包。
I want to calculate the frequency of occurrence of chars in a message using Perl. For instance, if the char "a" appears 10 times in a message, then the frequency would be 10. To do this, I am reading the message from a FILE one char at a time using the getc function. Here's the snippet I have written. It's very basic, I know. But when I compile, I get an error:
Details:
#!/usr/bin/perl
use strict;
use warnings;
my $input=$ARGV[0];
open(INPUT,"<$input");
while(<INPUT>
{
my $c=getc(INPUT);
print $c."\n";
}
close(INPUT);
I get the below error when I try to compile it:
Use of uninitialized value in print at AccessChar.pl line 13, <INPUT> line 1.
I am not able to figure out, what's wrong with this script. Could someone help me out with this?
I have even tried using getc
INPUT instead of getc(INPUT)
. I don't think I need to include any other packages while running this script.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将在循环的每次迭代中从
INPUT
读取整行。如果您想一次处理一个字符该文件句柄,那么它不是正确的结构。尝试类似的方法:
will read a whole line from
INPUT
at each iteration of the loop. If you want to process that file handle one char at a time, it is not the right construct to use.Try something like:
将文件读取运算符 (
< ... >
) 与getc
混合使用是一个坏主意。它没有按照你的想法去做。尝试将一些调试输出放入程序中以查看发生了什么。我通过运行程序本身来测试该程序(
./getc getc
)。在
while
循环开始时,从文件中读取一行并将其存储在
$_
中。然后,您可以使用getc
从文件中读取下一个字符。这将是文件第二行的第一个字符(可能是换行符 - 它可能是该行上的唯一字符)。下一次循环时,
读取下一行输入。这就是
use strict
行。getc
读取下一个字符,即来自use warnings
的“u”。如此一直持续到文件末尾。
读取一行,然后
getc
读取下一行的第一个字符。那根本不是你想要的。如果您想一次读取一个字符,那么您只需要
getc
。另一种选择是只使用
< ... >
并在阅读时分割每一行。但混合这两种方法是行不通的。
Mixing the file read operator (
< ... >
) withgetc
is a bad idea. It's not doing what you think it is.Try putting some debug output into the program to see what's going on. I tested the program by running it on itself (
./getc getc
).At the start of the
while
loop, the<INPUT>
reads a line from your file and stores it in$_
. You then usegetc
to read the next character from the file. That will be the first character from the second line of your file (probably a newline character - which is likely to be the only character on that line).The next time round the loop, the
<INPUT>
reads the next line of input. That's theuse strict
line. Thegetc
reads the next character which is the 'u' fromuse warnings
.And so it continues to the end of the file. The
<INPUT>
reads a line and then thegetc
reads the first character from the next line.That's not what you want at all. If you want to read a character at a time then you just need the
getc
.Another alternative would be to just use
< ... >
and split each line as you read it.But mixing the two approached is never going to work.
只是为了一点 TIMTOWTDI:
只要文件不是太大而无法吞咽,它就可以工作;如果是读取并分割每一行。用法:
Just for a little TIMTOWTDI:
which works as long as the file isn't too large to slurp; if it is the read and split each line. Usage: