954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Problem with phonebook -- hash

Hi,

I'm trying to write a perl program for a simple phone book. Means when the user enter a name, the phone book will search and display the name and the phone number of that person.

Below is my code:

#!/usr/bin/perl;
#playplay.pl

use warnings;
use strict;

my %phonebook = (
    dog => "7777777",
    cat => "8888888",
    monster => "99999999",
);

print ("Please Enter a name to be search:\t");
$nama = <STDIN>;

if($phonebook{$nama})
{
     print ("The phone number of $nama is $phonebook{$nama}");
}

else
{
     print ("The phone number of $nama cannot be found.");
}


1. I just cannot look-up for a person's contact using variable. If use "dog" or "cat" directly to search, the program run ok.

2. The second problem is that i cant display the last 2 statements (line 20 & 25) in one line.

Thanks.

raul15791
Junior Poster
102 posts since Jun 2008
Reputation Points: 37
Solved Threads: 7
 

add chomp($nama); after line 14. Because, you need to remove whitespaces(i.e, new-line character in this case) after you take input from keyboard.

Also you need to lexically scope $nama scalar. my $nama = <STDIN>;

katharnakh.

katharnakh
Posting Whiz in Training
237 posts since Jan 2006
Reputation Points: 19
Solved Threads: 34
 

One another thing forgot to mention in the last post, the way you check for existence of a key in hash(phonebook) is not right way. Because if you take this eg: then will come to know

my %h = (a=>undef);
if ($h{a}){print $h{a};}  # this way you check, but it should like,
if (exists $h{a}){print $h{a};#but the value is undefined, so you can check this as well and print some useful message}


katharnakh.

katharnakh
Posting Whiz in Training
237 posts since Jan 2006
Reputation Points: 19
Solved Threads: 34
 

Thanks katharnakh,

I just realise the funciton chomp(). Anyway, the checking of existence of keys in the proper way is very useful indeed. Thanks again.

raul15791
Junior Poster
102 posts since Jun 2008
Reputation Points: 37
Solved Threads: 7
 

One another thing forgot to mention in the last post, the way you check for existence of a key in hash(phonebook) is not right way. Because if you take this eg: then will come to know

my %h = (a=>undef);
if ($h{a}){print $h{a};}  # this way you check, but it should like,
if (exists $h{a}){print $h{a};#but the value is undefined, so you can check this as well and print some useful message}

katharnakh.

I think your example is a little misleading. If all he did was check for the existance of the hash key and it is undefined (or has a value of undef) he will get a false positive if the key exists but has no value.

#!/usr/bin/perl;
#playplay.pl

use warnings;
use strict;

my %phonebook = (
    dog => "7777777",
    cat => "8888888",
    monster => "99999999",
    cow => undef,
);

#print ("Please Enter a name to be search:\t");
$nama = 'cow';

if(exists $phonebook{$nama})
{
     print ("The phone number of $nama is $phonebook{$nama}");
}

else
{
     print ("The phone number of $nama cannot be found.");
}


But if the code is run as originally coded it will produce the correct output:

#!/usr/bin/perl;
#playplay.pl

use warnings;
use strict;

my %phonebook = (
    dog => "7777777",
    cat => "8888888",
    monster => "99999999",
    cow => undef,
);

#print ("Please Enter a name to be search:\t");
$nama = 'cow';

if($phonebook{$nama})
{
     print ("The phone number of $nama is $phonebook{$nama}");
}

else
{
     print ("The phone number of $nama cannot be found.");
}


Even if "cow" was not a key in the hash it would still work correctly. "exists" should be used to see if a key is in a hash, not to see if there is a value associated with the key. An exception might be a multi-level hash where you don't want the hash key to autovivify if you only check for its value. See the "exists" function manpage for more information.

Or maybe I just misunderstood the point you were making.

KevinADC
Posting Shark
921 posts since Mar 2006
Reputation Points: 246
Solved Threads: 67
 

Well, i try to search for some info on the EXISTS, DEFINED. What i found is that if we use
if (exists EXPR )
#it will return true if the if the specified element in the hash or array has ever been initialized, even if the corresponding value is undefined.

as for the DEFINED
if (defined EXPR )
#it will return true if the if the specified element in the hash or array is defined and the element can only be defined if it exists. This is not recommended to be used on aggregates (hashes and arrays). (**Why??)

if we use either if( defined EXPR ) or just if (EXPR), it will just display "contact cannot be found". but if we use (exists EXPR ), it will show some msg saying that value $phonebook{"cow"} is now initialized.

raul15791
Junior Poster
102 posts since Jun 2008
Reputation Points: 37
Solved Threads: 7
 
or maybe I just misunderstood the point you were making.


I think you would have missed the comment made in the code posted by me. my point was was simple,

%h = {a=>undef};
if (exists $h{a}){
	if($h{a}){
		print $h{a};
	}else{
		print 'Key exists, but value is undefined.'; # or Entry exists but phone no. is unavailable, for eg.
	}
}
as for the DEFINED if (defined EXPR ) #it will return true if the if the specified element in the hash or array is defined and the element can only be defined if it exists. This is not recommended to be used on aggregates (hashes and arrays). (**Why??)


Because, it is deperecated in Perl5.8.8(have no idea on earlier versions). It is said in perldoc. if(defined @an_array) or if(defined %a_hash) used to check whether memory for that aggregate has ever allocated. But this can also be achieved like,

# eg: from perldoc
if (@an_array) { print "has array elements\n" }
if (%a_hash)   { print "has hash members\n"   }


katharnakh.

katharnakh
Posting Whiz in Training
237 posts since Jan 2006
Reputation Points: 19
Solved Threads: 34
 
I think you would have missed the comment made in the code posted by me. my point was was simple,

ahhh.... yes, sorry. I missed the comment.

KevinADC
Posting Shark
921 posts since Mar 2006
Reputation Points: 246
Solved Threads: 67
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You