| < Day Day Up > |
Useful Things to Do with a Hash
Hashes are often used in Perl for more reasons than to store records by keys for later retrieval. The advantages to using a hash are fast individual access to keys and the fact that all keys in a hash are unique. These properties lend
Determining Frequency DistributionsIn Hour 6, "Pattern Matching," you learned how to take a line of text and split it into words. Examine the following snippet of code:
while ( <> ) {
while ( /(\w[\w-]*)/g ) { # Iterate over words, setting to each.
$Words{}++;
}
}
The first line reads the standard input one line at a time, setting $_ to each line.
The
The next line, although short, is where the snippet gets interesting.
$1
is set, in
When you're finished, the hash %Words contains a frequency distribution of the words read in. To look at the frequency distribution, you can use the following code:
foreach ( keys %Words ) {
print "$_ $Words{$_}\n";
}
Finding Unique Elements in Arrays
The technique shown in the
If your input text were the opening lines of One Fish, Two Fish , the list would look something like the following:
@fishwords=('one', 'fish', 'two', 'fish', 'red', 'fish', 'blue', 'fish');
If you are given this list of words (in
@fishwords
), and you need only the unique elements of the list, a hash works
Listing 7.1. Finding Unique Elements in an Array
1: %seen = ();
2: foreach (@fishwords) {
3: $seen{$_} = 1;
4: }
5: @uniquewords = keys %seen;
|
| < Day Day Up > |
Exercise: Creating a Simple Customer Database with Perl
When you call a customer service center and finally make it through the Touch-Tone
For this exercise, you're going to search a customer database. This program assumes that the database already exists, and it doesn't provide any way to update that database ”yet. Here, you're going to allow the
To begin this exercise, you need some data. Fire up your text editor, key in the text in Listing 7.4 (or something similar), and save it as
customers.txt
. Don't worry about the number of spaces between the
Listing 7.4. Sample Data for the Customer ProgramSmith,John (248)-555-9430 jsmith@aol.com Hunter,Apryl (810)-555-3029 april@showers.org Stewart,Pat (405)-555-8710 pats@starfleet.co.uk Ching,Iris (305)-555-0919 iching@zen.org Doe,John (212)-555-0912 jdoe@morgue.com Jones,Tom (312)-555-3321 tj2342@aol.com Smith,John (607)-555-0023 smith@pocahontas.com Crosby,Dave (405)-555-1516 cros@csny.org Johns,Pam (313)-555-6790 pj@sleepy.com Jeter,Linda (810)-555-8761 netless@earthlink.net Garland,Judy (305)-555-1231 ozgal@rainbow.com In the same directory, key in the short program in Listing 7.5 and save it as Customer . As usual, don't type the line numbers, and, if you can, be sure to make the program executable according to the instructions you learned in Hour 1, "Introduction to the Perl Language." When you're done, try running the program by typing the following at a command line:
Customer
or, if you cannot make the program executable on your system,
perl -w Customer
Listing 7.5. Complete Listing of the Customer Program
1: #!/usr/bin/perl -w
2:
3: open(PH, "customers.txt") or die "Cannot open customers.txt: $!\n";
4: while(<PH>) {
5: chomp;
6: ($number, $email) = ( split(/\s+/, $_) )[1,2];
7: $Phone{$number} = $_;
8: $Email{$email} = $_;
9: }
10: close(PH);
11:
12: print "Type 'q' to exit\n";
13: while (1) {
14: print "\nNumber? ";
15: $number = <STDIN>; chomp($number);
16: $address = "";
17: if (! $number ) {
18: print "E-Mail? ";
19: $address = <STDIN>; chomp($address);
20: }
21:
22: next if (! $number and ! $address);
23: last if ($number eq 'q' or $address eq 'q');
24:
25: if ( $number and exists $Phone{$number} ) {
26: print "Customer: $Phone{$number}\n";
27: next;
28: }
29:
30: if ($address and exists $Email{$address} ) {
31: print "Customer: $Email{$address}\n";
32: next;
33: }
34: print "Customer record not found.\n";
35: next;
36: }
36: print "\nAll done.\n";
Listing 7.6 shows the output of the Customer program. Listing 7.6. Sample Output from CustomerType 'q' to exit Number? <return> E-Mail? cros@csny.org Customer: Crosby, Dave (405)-555-1516 cros@csny.org Number? (305)-555-0919 Customer: Ching,Iris (305)-555-0919 iching@zen.org Number? q All done.
This example
|
| < Day Day Up > |