in reply to Re^2: Don't re-read file for each new pattern...
in thread Don't re-read file for each new pattern...
use strict; my $filename = '/home/cgmd/bin/learning_perl/sample_text'; open FILE, $filename or die "Can't open '$filename': $!"; chomp(my @strings = <FILE>); print "Please enter a pattern: "; while ((my $pattern = <STDIN>)!~/^\s$/) { chomp $pattern; my @matches = eval { grep /$pattern/, @strings; }; if ($@) { print "Error: $@"; } else { my $count = @matches; print "There were $count matching strings:\n", map "$_\n", @matches; print "Please enter a pattern: "; } print "\n"; }
In this version the while condition is not infinite, it breaks when the input pattern is empty.
citromatik
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Don't re-read file for each new pattern...
by cgmd (Beadle) on May 30, 2007 at 23:34 UTC |