Another Example: Counting

Here's an example of a script that makes use of that patterns-in-loops feature I just mentioned to work through a file (or any numbers of files) and count the incidences of some pattern in that file. With this script you could, for example, count the number of times your name occurs in a file, or find out how many hits to your Web site came from America Online (aol.com). I ran it on a draft of this lesson and found that I've used the word pattern 184 times so far.

Listing 9.2 shows this simple script:

Listing 9.2. count.pl
1:  #!/usr/bin/perl -w
2:
3:  $pat = ""; # thing to search for
4:  $count = 0; # number of times it occurs
5:
6:  print 'Search for what? ';
7:  chomp($pat = <STDIN>);
8:  while (<>) {
9:      while (/$pat/g) {
10:         $count++;
11:     }
12: }
13:
14: print "Found /$pat/ $count times.
";

As with all the scripts we've built that cycle through files using <>, you'll have to call this one on the command line with the name of a file:

% count.pl logfile
Search for what? aol.com
Found /aol.com/ 3456 times.
%

Nothing in Listing 9.2 should look overly surprising, although there are a few points to note. Remember that using while with the file input characters (<>) sets each line of input to the default variable $_. Because patterns will also match with that value by default, we don't need a temporary variable to hold each line of input. The first while loop (line 8), then, reads each line from the input files. The second while loop searches that single line of input repeatedly and increments $count each time it finds the pattern in each line. This way, we can get the total number of instances of the given pattern, both inside each line and for all the lines in the input.

One other important thing to note about this script is if you have it search for a phrase instead of a single word—for example, find all instances of both a first and last name—then there is a possibility that that phrase could fall across multiple lines. This script will miss those instances because neither line will completely match the pattern. Tomorrow, you'll learn how to search for a pattern that can fall on multiple lines.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset