distro has asked for the wisdom of the Perl Monks concerning the following question:

I need help with my program, I had everything working but then when I went back to try it out my second if statement was failing every time.

Anyone know what to do instead?

If statement

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

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;

Thanks!

Replies are listed 'Best First'.
Re: Need help with an if statement
by choroba (Cardinal) on Nov 17, 2016 at 01:14 UTC
    Crossposted to StackOverflow. It's considered polite to inform about crossposting to eliminate duplicate effort by people not visiting both sites.

    Your usage of the first -e is correct, it checks the existence of a file. The second usage seems to interpret -e as a search of the given word in the file named $filename. How could Perl know what interpretation to use when, how should it guess what file to search in the latter case?

    See also open and readline.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Need help with an if statement
by beech (Parson) on Nov 17, 2016 at 02:44 UTC

    Hi,

    try this approach by completing this program,

    the ... is the part you write, refer to perlintro and Basic debugging checklist as your study notes.

    When you're finished or when you get stuck , post what you have , ask a question

    #!/usr/bin/perl -- use strict; use warnings; our %messages = ( welcome => " Welcome... ", promptfile => "Please enter the name of the file to search:", promptword => "Enter a word to search for:", byenofile => "No such file exists. Exiting program. Please try aga +in.\n", byenoword => "No such word found.\n", wordfrequency => "...", ); ## debugging WordFrequency exit WordFrequency( \"this is in memory test file with test words", "t +est" ); my $file = Prompt( $messages{welcome}.$messages{promptfile} ); my $word = Prompt( $messages{promptword} ); WordFrequency( $file, $word ); exit; sub WordFrequency { my( $file , $word ) = @_; open my($fh), '<', $file or die $messages{byenofile}; my $freq = 0; ...; if( $freq ){ print $messages{wordfrequency}, $freq, "\n"; } else { print $messages{byenoword}; } return $freq; } sub Prompt { ... }