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.

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.

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.

Comments
short and nice

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.

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.

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.

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.

Comments
helpful

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.

This question has already been answered. Start a new discussion instead.