Ich brauche Hilfe bei der Anpassung dieser Block meines Codes. Alles funktionierte, aber dann hörte es auf zu arbeiten und versagte jedes Mal (Drucke). Was mache ich falsch?Brauchen Sie einen Vorschlag mit 'if' Anweisung
print "Enter a word to search for:";
chomp (my $word = <STDIN>);
if (not -e $word){
print "No such word found.\n";
exit;
}
Ganzes Programm.
#!/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;
Was erwarten Sie von dieser Linie? Weil es prüft, ob eine Datei nicht existiert, was im Kontext nicht sinnvoll ist. Und diese "Trennung" ist eine wirklich seltsame Art, Wörter aus einer Datei zu lesen ... wenn es überhaupt funktioniert. – Schwern
http://www.perlmonks.org/?node_id=1176031 – choroba