mercredi 16 novembre 2016

Need suggestion with 'if' statement

I need help adjusting this block of my code. Everything was working but then it quit working and fails every time (prints). What am I doing wrong?

print "Enter a word to search for:";
chomp (my $word = <STDIN> );
if (not -e $word){
        print "No such word found.\n";
        exit;
}

Whole program.

#!/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;

Aucun commentaire:

Enregistrer un commentaire