为什么我的程序使用 Perl 的 getc 函数无法正常工作?

发布于 2024-12-10 07:49:46 字数 644 浏览 1 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

昵称有卵用 2024-12-17 07:49:46
while (<INPUT>)

将在循环的每次迭代中从 INPUT 读取整行。如果您想一次处理一个字符该文件句柄,那么它不是正确的结构。

尝试类似的方法:

my $c;
while (defined($c = getc(INPUT))) {
  print $c, "\n";
}
while (<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:

my $c;
while (defined($c = getc(INPUT))) {
  print $c, "\n";
}

将文件读取运算符 (< ... >) 与 getc 混合使用是一个坏主意。它没有按照你的想法去做。

尝试将一些调试输出放入程序中以查看发生了什么。我通过运行程序本身来测试该程序(./getc getc)。

while 循环开始时, 从文件中读取一行并将其存储在 $_ 中。然后,您可以使用 getc 从文件中读取下一个字符。这将是文件第二行的第一个字符(可能是换行符 - 它可能是该行上的唯一字符)。

下一次循环时, 读取下一行输入。这就是 use strict 行。 getc 读取下一个字符,即来自 use warnings 的“u”。

如此一直持续到文件末尾。 读取一行,然后 getc 读取下一行的第一个字符。

那根本不是你想要的。如果您想一次读取一个字符,那么您只需要getc

#!/usr/bin/perl

use strict;
use warnings;

my $input = shift;

open(my $file, '<', $input);

while (defined(my $c = getc $file)) {
  print "$c\n";
}

另一种选择是只使用 < ... > 并在阅读时分割每一行。

#!/usr/bin/perl

use strict;
use warnings;

my $input = shift;

open(my $file, '<', $input);

while (<$file>) {
  foreach my $c (split //) {
    print "$c\n";
  }
}

但混合这两种方法是行不通的。

Mixing the file read operator (< ... >) with getc 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 use getc 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 the use strict line. The getc reads the next character which is the 'u' from use warnings.

And so it continues to the end of the file. The <INPUT> reads a line and then the getc 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.

#!/usr/bin/perl

use strict;
use warnings;

my $input = shift;

open(my $file, '<', $input);

while (defined(my $c = getc $file)) {
  print "$c\n";
}

Another alternative would be to just use < ... > and split each line as you read it.

#!/usr/bin/perl

use strict;
use warnings;

my $input = shift;

open(my $file, '<', $input);

while (<$file>) {
  foreach my $c (split //) {
    print "$c\n";
  }
}

But mixing the two approached is never going to work.

衣神在巴黎 2024-12-17 07:49:46

只是为了一点 TIMTOWTDI:

#!/usr/bin/env perl

use strict;
use warnings;

use Data::Dumper;

local $/;
my %chars;

$chars{$_}++ for split //, <>;

print Dumper \%chars;

只要文件不是太大而无法吞咽,它就可以工作;如果是读取并分割每一行。用法:

$ count_chars.pl file_to_read

Just for a little TIMTOWTDI:

#!/usr/bin/env perl

use strict;
use warnings;

use Data::Dumper;

local $/;
my %chars;

$chars{$_}++ for split //, <>;

print Dumper \%chars;

which works as long as the file isn't too large to slurp; if it is the read and split each line. Usage:

$ count_chars.pl file_to_read
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文