< Day Day Up > |
When you call a customer service center and finally make it through the Touch-Tone menus , the first thing the human being on the other end of the phone asks you for is your telephone number. Well, almost every time. Sometimes the customer service representative wants your customer number or even your Social Security number. What this person is after is something that uniquely identifies you to the computer he or she is using. These numbers serve as keys for retrieving information about you in a database. Sounds like Perl's hashes, doesn't it? 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 user to search on one of two different fields. 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 columns or aligning them, as long as you leave at least one space between each column. 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 Program1: #!/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 demonstrates a few Perl features. Hashes are used for quick lookups of data based on a key. Because Perl implements hashes very efficiently , response time for a query should not become inefficient even if this program has thousands or tens of thousands of records in the hashes. Also, this program serves as a demonstration of program flow control using a simpleblock instead of other control structures ( while , do , until , and so on). |
< Day Day Up > |