dmtelf has asked for the wisdom of the Perl Monks concerning the following question:
The first two tests both return "Unknown Caller". I think the hash has been built and populated correctly as the last two tests work. The first test should return "Tom". The second test should return 'Unknown Caller'.
The problem appears to be in &MatchPhoneBook which returns either a user's name if their number is in the hash or 'Unknown caller'. It currently always returns 'Unknown Caller', no matter what number is fed to it.
Can some Monk please explain why the first two tests do not work as expected? What am I doing wrong?
thanks
dmtelf
use strict; my (%PhoneBook,$TheCaller); my ($ValidTest,$InvalidTest); my ($name,$NumToFind); # Read in CSV phone book &ReadPhoneBookFile; # Run two parameter tests $ValidTest = "8954"; $InvalidTest = "9999"; print "Parameter test 1 - Number to find is: $ValidTest\n"; $TheCaller = &MatchPhoneNumber($ValidTest); print "The caller was: $TheCaller\n"; print "\nParameter test 2 - Number to find is: $InvalidTest\n"; $TheCaller = &MatchPhoneNumber($InvalidTest); print "The caller was: $TheCaller\n"; # Run two direct tests. # First one works, second one doesn't. print "\nDirect test 1 (8594) - ".$PhoneBook{8594}; # WORKS! $NumToFind = "8594"; print "Direct test 2 (8594) - ".$PhoneBook{$NumToFind}; # DOES NOT +WORK! exit; sub ReadPhoneBookFile ### ### Reads in a CSV separated phone book, splits, stuffs details into +a hash ### ### In real-world script, data is read from an external file. { my ($Name,$Number,@Details); foreach (<DATA>) { @Details = split(/,/,$_); $Number = $Details[0]; $Name = $Details[1]; $PhoneBook{$Number} = $Name; } } sub MatchPhoneNumber ### ### Attempts to match the phone number with a name from the phonebook ### Either returns the caller name if number in hash or "Unknown calle +r". ### { my $NumToFind = shift; my $TheName; $TheName = defined $PhoneBook{$NumToFind} ? $PhoneBook{$NumToFind} + : 'Unknown Caller'; return $TheName; } __DATA__ 8594,Tom 9000,Dick 1234,Harry
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Data extraction from hash problem
by maverick (Curate) on Aug 07, 2000 at 00:18 UTC | |
|
Re: Data extraction from hash problem
by tilly (Archbishop) on Aug 07, 2000 at 00:23 UTC | |
|
RE: Data extraction from hash problem
by Russ (Deacon) on Aug 07, 2000 at 01:05 UTC | |
|
RE: Data extraction from hash problem
by Russ (Deacon) on Aug 07, 2000 at 00:14 UTC | |
by mwp (Hermit) on Aug 07, 2000 at 00:24 UTC | |
by tilly (Archbishop) on Aug 07, 2000 at 00:34 UTC | |
|
Re: Data extraction from hash problem
by markguy (Scribe) on Aug 07, 2000 at 21:21 UTC |