#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %phone_num; while (my $line =) { my $name; my $phone; # this throws away malformed lines, like "4 (represents..." next unless ($name,$phone) = $line =~/^\s*([a-zA-Z]+)\s+(\d+)?/; if (defined $phone) # A new phone book entry { $phone_num{$name} = $phone; } else # Just a name. Look up its phone number { if ($phone_num{$name}) { print "$name $phone_num{$name}\n"; } else { print "Not found\n"; } } } print "\nJust to show what phone_num hash is:\n"; print Dumper \%phone_num; =Program Output: Not found john 334455 Not found Just to show what phone_num hash is: $VAR1 = { 'harry' => '112233', 'tom' => '332211', 'ryan' => '445566', 'john' => '334455' }; =cut __DATA__ 4 (represents no. of entries in phone directory) tom 332211 harry 112233 ryan 445566 john 334455 jack john jay 4576654 this is also junk line (just a number)