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