in reply to problem printing message if search item is not found

Since you are new to Perl and currently forming some habits, consider adding the following to your scripts:
use strict; use warnings;
Over the long run these optional settings will help point out many small problems in your code. They wouldn't have helped you with your current question, but may head off some future ones.

Replies are listed 'Best First'.
Re^2: problem printing message if search item is not found
by omega_monk (Scribe) on Jun 20, 2005 at 15:37 UTC
    Thought it might be helpful to see a rewrite of the code. ;)

    Just cleaned up a bit, and added use strict; and use warnings; and a little bit of error handling.
    #!/usr/bin/perl use strict; use warnings; my $fsearch = 'file.txt'; open (TEXTFILE, 'c:/perl_programs/' . $fsearch) or die "CAN'T OPEN $fs +earch: $^E\n"; print "Enter a string to search for: "; chomp (my $search = <STDIN>); my @line = <TEXTFILE>; my $count = 0; foreach (@line){ $count++; if ($_ =~ /$search/i) { print "\nLine that matched <$search> found on line #$count\n"; print "$_\n"; } } if (!$count) { print "\nsubstring <$search> NOT found\n"; }