Мне нужна помощь в настройке этого блока моего кода. Все работало, но потом оно перестало работать и терпело неудачу каждый раз (отпечатки). Что я делаю не так?Нужно предложение с оператором 'if'
print "Enter a word to search for:";
chomp (my $word = <STDIN>);
if (not -e $word){
print "No such word found.\n";
exit;
}
Всего программ.
#!/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:";
chomp (my $filename = <STDIN>);
if (not -e $filename){
print "No such file exists. Exiting program. Please try again.
+\n";
exit;
}
print "Enter a word to search for:";
chomp (my $word = <STDIN>);
if (not -e $word){
print "No such word found.\n";
exit;
}
print "Frequency of word: " . grep $word eq $_,
split /\W+/i, do { local (@ARGV, $/)= $filename; <> };
exit;
Что вы ожидаете от этой линии? Потому что он проверяет, нет ли файла, который не имеет смысла в контексте. И этот 'split' - это действительно странный способ чтения слов из файла ... если он вообще работает. – Schwern
http://www.perlmonks.org/?node_id=1176031 – choroba