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 prints each line of its input that mentions fred. (It
4 ffd9a51f 2023-08-04 jrmu # shouldn't do anything for other lines of input.) Does it match if your
5 ffd9a51f 2023-08-04 jrmu # input string is Fred, frederick, or Alfred? Make a small text file with a
6 ffd9a51f 2023-08-04 jrmu # few lines mentioning "fred flintstone" and his friends, then use that file
7 ffd9a51f 2023-08-04 jrmu # as input to this program and the ones later in this section.
8 ffd9a51f 2023-08-04 jrmu
9 ffd9a51f 2023-08-04 jrmu use warnings;
10 ffd9a51f 2023-08-04 jrmu use strict;
11 ffd9a51f 2023-08-04 jrmu use utf8;
12 ffd9a51f 2023-08-04 jrmu
13 ffd9a51f 2023-08-04 jrmu foreach (grep(/fred/, <>)) {
14 ffd9a51f 2023-08-04 jrmu print "$_";
15 ffd9a51f 2023-08-04 jrmu }
16 ffd9a51f 2023-08-04 jrmu
17 ffd9a51f 2023-08-04 jrmu #my @words = <>;
18 ffd9a51f 2023-08-04 jrmu #my @matches = grep(/fred/, @words);
19 ffd9a51f 2023-08-04 jrmu #foreach (@matches) {
20 ffd9a51f 2023-08-04 jrmu # print "$_";
21 ffd9a51f 2023-08-04 jrmu #}