in reply to Data extraction from hash problem
Oh, well.
ReadPhoneBookFile() cannot "see" %PhoneBook, because %PhoneBook is lexical.
Okay, here is the code after I made a few changes to it.
A few notes:use strict; use vars qw/%PhoneBook/; # Read in CSV phone book ReadPhoneBookFile(); # Run two parameter tests my $ValidTest = "8594"; my $InvalidTest = "9999"; print "Parameter test 1 - Number to find is: $ValidTest\n"; print 'The caller was: ', MatchPhoneNumber($ValidTest), "\n"; print "\nParameter test 2 - Number to find is: $InvalidTest\n"; print 'The caller was: ', MatchPhoneNumber($InvalidTest), "\n"; 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. { foreach (<DATA>) { my @Details = split(/,/,$_); $PhoneBook{$Details[0]} = $Details[1]; } } 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; return defined $PhoneBook{$NumToFind} ? $PhoneBook{$NumToFind} : 'Unknown Caller'; } __DATA__ 8594,Tom 9000,Dick 1234,Harry
Russ
Brainbench 'Most Valuable Professional' for Perl
|
|---|