Blame


1 ffd9a51f 2023-08-04 jrmu #!/usr/bin/perl
2 ffd9a51f 2023-08-04 jrmu
3 ffd9a51f 2023-08-04 jrmu # Make a program that reads a list of strings from a file, one
4 ffd9a51f 2023-08-04 jrmu # string per line, and then lets the user interactively enter
5 ffd9a51f 2023-08-04 jrmu # patterns that may match some of the strings. For each pattern,
6 ffd9a51f 2023-08-04 jrmu # the program should tell how many strings from the file matched,
7 ffd9a51f 2023-08-04 jrmu # then which ones those were. Don't reread the file for each new
8 ffd9a51f 2023-08-04 jrmu # pattern; keep the strings in memory. The filename may be
9 ffd9a51f 2023-08-04 jrmu # hardcoded in the file. If a pattern is invalid (for example, if
10 ffd9a51f 2023-08-04 jrmu # it has unmatched parentheses), the program should simply report
11 ffd9a51f 2023-08-04 jrmu # that error and let the user continue trying patterns. When the
12 ffd9a51f 2023-08-04 jrmu # user enters a blank line instead of a pattern, the program should
13 ffd9a51f 2023-08-04 jrmu # quit. (If you need a file full of interesting strings to try
14 ffd9a51f 2023-08-04 jrmu # matching, try the file sample_text in the files you've surely
15 ffd9a51f 2023-08-04 jrmu # downloaded by now from the O'Reilly website; see Chapter 1.)
16 ffd9a51f 2023-08-04 jrmu
17 ffd9a51f 2023-08-04 jrmu use v5.24;
18 ffd9a51f 2023-08-04 jrmu use warnings;
19 ffd9a51f 2023-08-04 jrmu use strict;
20 ffd9a51f 2023-08-04 jrmu use utf8;
21 ffd9a51f 2023-08-04 jrmu
22 ffd9a51f 2023-08-04 jrmu if (scalar(@ARGV) != 1) {
23 ffd9a51f 2023-08-04 jrmu die "Usage: $0 file\n";
24 ffd9a51f 2023-08-04 jrmu }
25 ffd9a51f 2023-08-04 jrmu
26 ffd9a51f 2023-08-04 jrmu my $file = shift @ARGV;
27 ffd9a51f 2023-08-04 jrmu
28 ffd9a51f 2023-08-04 jrmu open my $fh, '<', $file or die "Unable to open '$file': $!";
29 ffd9a51f 2023-08-04 jrmu chomp(my @lines = <$fh>);
30 ffd9a51f 2023-08-04 jrmu my $pattern;
31 ffd9a51f 2023-08-04 jrmu
32 ffd9a51f 2023-08-04 jrmu print "Pattern (blank to quit): ";
33 ffd9a51f 2023-08-04 jrmu while (chomp($pattern = <>) && $pattern !~ /\A\s*\z/) {
34 ffd9a51f 2023-08-04 jrmu my @matches = eval {
35 ffd9a51f 2023-08-04 jrmu grep /$pattern/, @lines;
36 ffd9a51f 2023-08-04 jrmu };
37 ffd9a51f 2023-08-04 jrmu if ($@) {
38 ffd9a51f 2023-08-04 jrmu print "'$pattern' is not a valid regular expression!\n";
39 ffd9a51f 2023-08-04 jrmu } else {
40 ffd9a51f 2023-08-04 jrmu printf("%s: %s has %d matches\n", $file, $pattern, scalar(@matches));
41 ffd9a51f 2023-08-04 jrmu print join("\n",@matches);
42 ffd9a51f 2023-08-04 jrmu print "\n";
43 ffd9a51f 2023-08-04 jrmu }
44 ffd9a51f 2023-08-04 jrmu print "Pattern: ";
45 ffd9a51f 2023-08-04 jrmu }
46 ffd9a51f 2023-08-04 jrmu
47 ffd9a51f 2023-08-04 jrmu # When the user enters a blank line instead of a pattern, the
48 ffd9a51f 2023-08-04 jrmu # program should quit. (If you need a file full of interesting
49 ffd9a51f 2023-08-04 jrmu # strings to try matching, try the file sample_text in the files
50 ffd9a51f 2023-08-04 jrmu # you've surely downloaded by now from the O'Reilly website; see
51 ffd9a51f 2023-08-04 jrmu # Chapter 1.)