in reply to Re: Don't re-read file for each new pattern...
in thread Don't re-read file for each new pattern...

I'm interested, but not sure how I would use...
perl -e 'while (<>!~/^\s$/){print "Not empty\n"}'
...in the script I provided.

Could you please elaborate?

Thanks!

Replies are listed 'Best First'.
Re^3: Don't re-read file for each new pattern...
by citromatik (Curate) on May 30, 2007 at 17:06 UTC
    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

      Now, I see how it fits in!

      Thanks for demonstrating it in use...