K
Krishna Chaitanya
Hi,
Am trying the exercise in Chapter 17 of Learning Perl....for those who
don't have that book, here is the question:
"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 program should tell
how many strings from the file matched, then which ones those were.
Don’t reread the file for each new pattern; keep the strings in
memory. The filename may be hardcoded in the file. If a pattern is
invalid (for example, if it has unmatched parentheses), the program
should simply report that error and let the user continue trying
patterns. When the user enters a blank line instead of a pattern, the
program should quit."
I came up with this code:
-----------
#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
open FH, "abc" or die "Cannot open file: $!\n";
my @file_contents = <FH>;
while(1) {
chomp(my $pattern = <STDIN>);
last if $pattern =~ /^\s*$/;
my @matched = grep {
my $line = $_;
eval { $line =~ /$pattern/ };
if ($@) {
print "Error in pattern : $@\n";
undef;
}
} @file_contents;
print "Number of matched lines = ", scalar @matched, "\n";
print "Matched lines are:\n @matched\n";
}
-------------------
It doesn't work...for simple patterns like "This", "cat", etc. Oh, and
file "abc" contains:
This is a cat
This is a dog
This is a ball
But it works if I put the eval outside the grep (as mentioned in the
solution to the exercise in that book). I thought the code block for
grep in my code above always returns either true/false......but it's
not working. Can you pls. help me by pointing out the mistake?
Am using Perl 5.8.8 on Linux, if it matters.
Thanks,
Chaitanya
Am trying the exercise in Chapter 17 of Learning Perl....for those who
don't have that book, here is the question:
"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 program should tell
how many strings from the file matched, then which ones those were.
Don’t reread the file for each new pattern; keep the strings in
memory. The filename may be hardcoded in the file. If a pattern is
invalid (for example, if it has unmatched parentheses), the program
should simply report that error and let the user continue trying
patterns. When the user enters a blank line instead of a pattern, the
program should quit."
I came up with this code:
-----------
#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
open FH, "abc" or die "Cannot open file: $!\n";
my @file_contents = <FH>;
while(1) {
chomp(my $pattern = <STDIN>);
last if $pattern =~ /^\s*$/;
my @matched = grep {
my $line = $_;
eval { $line =~ /$pattern/ };
if ($@) {
print "Error in pattern : $@\n";
undef;
}
} @file_contents;
print "Number of matched lines = ", scalar @matched, "\n";
print "Matched lines are:\n @matched\n";
}
-------------------
It doesn't work...for simple patterns like "This", "cat", etc. Oh, and
file "abc" contains:
This is a cat
This is a dog
This is a ball
But it works if I put the eval outside the grep (as mentioned in the
solution to the exercise in that book). I thought the code block for
grep in my code above always returns either true/false......but it's
not working. Can you pls. help me by pointing out the mistake?
Am using Perl 5.8.8 on Linux, if it matters.
Thanks,
Chaitanya