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

What segment of the script is designed to meet that requirement?
chomp(my @strings = <FILE>);

@strings is a variable stored in memory that contains the lines of FILE (one line per position in the array)

And could someone please explain if the infinite "while (1)" loop is required for this?

The infinite while asks for an input pattern, the only way of exiting from it is "typing" an empty pattern (i.e. /^\s*$/). Because the program doesn't know when the user will enter an empty pattern, it iterates forever checking if the pattern is empty.

citromatik

UPDATE: Of course, TIMTOWTDI:

perl -e 'while (<>!~/^\s$/){print "Not empty\n"}'

Replies are listed 'Best First'.
Re^2: Don't re-read file for each new pattern...
by cgmd (Beadle) on May 30, 2007 at 15:41 UTC
    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!

      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...