我是相当新的编码,我需要一个失败声明打印出来,就好像它是一个或死亡。作为例子来我的代码需要类似开放或死亡,除了与chomp
部分:
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>);
我需要做的是为这两个打印/格格语句。无论如何只要加上这个?
整个程序:
#!/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;
开始你的计划我有点在你的程序中的逻辑困惑,错误消息表明您正在做的事情有一个文件,但在这点它所做的就是从标准输入读取 – adrianp
我可以发布整个程序,但我不明白它会如何帮助,虽然考虑我的问题。 – distro
哦,这个更新改变了一点,不是吗。我的答案代表了从'<>'和'chomp'测试读取的原始问题。我会更新到这个新内容。 – zdim