This code snippet creates a hash from a CSV phonebook list (number,name). Sample data is at the end of the script. 4 tests are then run on this hash to see if the data is extracted correctly.

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

In reply to Data extraction from hash problem by dmtelf

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.