【问题标题】:Need something like open OR DIE except with chomp需要类似 open OR DIE 的东西,除了 chomp
【发布时间】:2016-11-16 22:58:49
【问题描述】:

我对编码还很陌生,我需要一个失败语句来打印出来,就好像它是一个或死一样。

我的部分代码示例:

    print "Please enter the name of the file to search:";
    chomp (my $filename=<STDIN>) or die "No such file exists. Exiting program. Please try again."\n;

    print "Enter a word to search for:";
    chomp (my $word=<STDIN>);

我需要它来为这两个 print/chomp 语句执行此操作。反正有什么可以补充的吗?

整个程序:

#!/usr/bin/perl -w

use strict;

print "Welcome to the word frequency calculator.\n";
print "This program prompts the user for a file to open, \n";
print "then it prompts for a word to search for in that file,\n";
print "finally the frequency of the word is displayed.\n";
print " \n";

print "Please enter the name of the file to search:";
while (<>){
        print;
}

print "Enter a word to search for:";
chomp( my $input = <STDIN> );

my $filename = <STDIN>;

my$ctr=0;
foreach( $filename ) {
        if ( /\b$input\b/ ) {
                $ctr++;
        }
}
print "Freq: $ctr\n";

exit;

【问题讨论】:

  • 我对您程序中的逻辑有点困惑,错误消息表明您正在对文件执行某些操作,但此时它所做的只是从标准输入中读取输入。
  • 我可以发布整个程序,但考虑到我的问题,我只是看不出它有什么帮助。
  • 哦,这次更新有点改变,不是吗。我的回答代表来自&lt;&gt;chomp 的原始测试问题。我会更新到这个新内容。

标签: perl chomp


【解决方案1】:

您不需要测试文件句柄读取&lt;&gt; 是否成功。见I/O Operators in perlop。当它没有要读取的内容时,它会返回一个undef,这正是您想要的,因此您的代码知道何时停止读取。

至于删除换行符,无论如何你都想单独chomp。否则,一旦读取确实返回undef,您将在未定义的变量上使用chomp,从而触发警告。

通常,在某些资源上打开文件句柄 $fh,您会这样做

while (my $line = <$fh>) {
    chomp $line;
    # process/store input as it comes ...
}

这也可以是STDIN。如果肯定只有一行

my $filename = <STDIN>;
chomp $filename;

您也不需要针对失败测试chomp。请注意,它会返回它删除的字符数,因此如果没有 $/(通常是换行符),它会合法地返回 0

补充一点,经常测试是一个很好的习惯!作为这种心态的一部分,请确保始终使用use warnings;,我也强烈建议使用use strict; 进行编码。


更新重要问题编辑

在第一个while 循环中,您不会将文件名存储在任何地方。鉴于打印的问候语,而不是那个循环,您应该只阅读文件名。然后您阅读要搜索的单词。

# print greeting

my $filename = <STDIN>;
chomp $filename;

my $input = <STDIN>;
chomp $input;

但是,我们遇到了更大的问题:您需要open 文件,然后才能逐行浏览并搜索单词。这是你需要测试的地方。请参阅链接的文档页面和教程perlopentut。首先检查是否存在同名的文件。

if (not -e $filename) {
    print "No file $filename. Please try again.\n";
    exit;
}

open my $fh, '<', $filename  or die "Can't open $filename: $!";

my $word_count = 0;
while (my $line = <$fh>) 
{
    # Now search for the word on a line
    while ($line =~ /\b$input\b/g) {
        $word_count++;
    }
}
close $fh  or die "Can't close filehandle: $!";

上面的-e 是文件测试之一,它检查给定文件是否存在。请参阅file-tests (-X) 的文档页面。在上面的代码中,我们只是退出并显示一条消息,但您可能希望在循环中打印提示用户输入另一个名称的消息。

我们在正则表达式中使用while/g 修饰符来查找一行中单词的所有出现。

我还强烈建议您始终使用以下方式启动您的程序

use warnings 'all';
use strict;

【讨论】:

  • 但我想要,我知道我不需要。
  • @distro 好的,这在原则上非常好 -- 但使用 chomp 你不会想要。它返回许多已删除的字符,因此如果未找到换行符,则返回零。因此,“正常”测试(使用or)会因错误原因触发。如果您只关心 failing 的内置函数调用,那么您几乎必须测试每个语句!
  • 好的,感谢您的建议。但是绝对没有办法做到这一点吗?如果用户放入的文件不存在,我希望我的程序退出并显示我自己的消息。
  • @distro 我正在将答案更新为您的编辑。那是另一回事,与chomp 无关。 chomp 仅适用于 string,在您的情况下包含文件名和换行符。它不处理实际文件。
  • 您可能想自己运行该程序,该程序运行良好我只是想知道一种方法使其退出并在第一个 chomp 语句中找不到文件时打印一条消息。也许这是不可能的,如果是这样,你会推荐我使用 isteand 吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多