"Make a program that reads a list of strings from a file, one string per line, and then lets the user interactively enter patterns that may match some of the strings. For each pattern,the pattern should tell how many strings from the file matched, then which ones those were. DON'T RE-READ THE FILE FOR EACH NEW PATTERN; KEEP THE STRINGS IN MEMORY. the filename...." The offered code to satisfy this example is:
My confusion is with the segment of the example (shown above in uppercase) regarding "keep the strings in memory". What segment of the script is designed to meet that requirement? And could someone please explain if the infinite "while (1)" loop is required for this?use strict; my $filename = '/home/cgmd/bin/learning_perl/sample_text'; open FILE, $filename or die "Can't open '$filename': $!"; chomp(my @strings = <FILE>); while (1) { print "Please enter a pattern: "; chomp(my $pattern = <STDIN>); last if $pattern =~ /^\s*$/; my @matches = eval { grep /$pattern/, @strings; }; if ($@) { print "Error: $@"; } else { my $count = @matches; print "There were $count matching strings:\n", map "$_\n", @matches; } print "\n"; }
Thanks!
In reply to Don't re-read file for each new pattern... by cgmd
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |