#!/usr/bin/perl use Term::ANSIColor; # clear the screen to start off system (clear); print color("red"), "###############################################################",color("reset"),"\n", color("white"), "Presenting: ",color("reset"),"\n", color("white"), " Exersize 5.9 ",color("reset"),"\n", color("red"), "###############################################################",color("reset"),"\n" ; print color("magenta"),"\tStore phone numbers in a hash.",color("reset"),"\n", color("yellow"),"\tGet phone numbers by typing in a name.",color("reset"),"\n"; # intitialize the hash which will hold the phone numbers from the file #-#%phonenumber = ( 'joe' =>408-345-3219, #-# 'nancy'=>304-346-7539, #-# 'foo'=>304-567-2123, #-# 'fe'=>304-587-1234, #-# 'fi'=>324-533-2343, #-# 'fum'=>305-234-2345 ); ######################################################### %phonenumber = ( ); $File="my_phonenumber.txt"; print color("magenta"),"\nPart 1: Opening a file, named ",color("green"),$File,color("magenta")," and populate the hash.",color("reset"),"\n" ; # open the data file and populate the hash open (PHONELIST, "<$File") || die "Could not open your phonelist:\t $!\n"; while (){ ($value,$key) =split(' ',$_); $phonenumber{$key}="$value"; } close (PHONELIST)|| die "Can't close my_phonenumber.txt: $!";; # Print the contents of the hash for diagnostic print "\nPrint contents of the hash to show that Part One succeded.\n"; foreach $name ( keys %phonenumber ){ print "$name\t=>\t",$phonenumber{$name},"\n"; } #--#print "\nPrint Hash Method 2: \n"; #--## Print the contents of the hash method 2 #--#while ( ($k,$v)= each %phonenumber) { #--# print "$k\t=>\t$v\n"; #--#} ######################################################### color("yellow"),"Part 2: Ask for the name to be looked up (the key), and print out the resulting phone number (the value).",color("reset"),"\n"; # Input the name to search for print "Who's phone number do you want?\t"; $name_search=; chomp ($name_search); # Does the hash have a value for key? if (exists($phonenumber{$name_search})){ print color("green"),"\n\t$phonenumber{$name_search}",color("reset"),"\n\n" ; } else { print color("red"),"\n\tThat name is not in my phone list.",color("reset"),"\n\n" ; } ############################################################### Presenting: Exersize 5.9 ############################################################### Store phone numbers in a hash. Get phone numbers by typing in a name. Part 1: Opening a file, named my_phonenumber.txt and populate the hash. Print contents of the hash to show that Part One succeded. foo => 304-567-2123 joe => 408-345-3219 fum => 305-234-2345 fi => 324-533-2343 nancy => 304-346-7539 fe => 304-587-1234 Who's phone number do you want? fe 304-587-1234 ############################################################### Presenting: Exersize 5.9 ############################################################### Store phone numbers in a hash. Get phone numbers by typing in a name. Part 1: Opening a file, named my_phonenumber.txt and populate the hash. Print contents of the hash to show that Part One succeded. foo => 304-567-2123 joe => 408-345-3219 fum => 305-234-2345 fi => 324-533-2343 nancy => 304-346-7539 fe => 304-587-1234 Who's phone number do you want? bla That name is not in my phone list.