use strict; my $filename = "words"; # hash: starts with %. my %digitWords; my %letterValues = ('a' => '2', 'b' => '2', 'c' => '2', 'd' => '3', 'e' => '3', 'f' => '3', 'g' => '4', 'h' => '4', 'i' => '4', 'j' => '5', 'k' => '5', 'l' => '5', 'm' => '6', 'n' => '6', 'o' => '6', 'p' => '7', 'q' => '7', 'r' => '7', 's' => '7', 't' => '8', 'u' => '8', 'v' => '8', 'w' => '9', 'x' => '9', 'y' => '9', 'z' => '9'); # IN: filehandler open IN, $filename; while ($_ = ) { chomp; my $word = lc($_); # Skip if there is non-alphabets next if /[^a-z]/; # Get the corresponding digit string. my $digits = ""; foreach $_ (split //, $word) { $digits .= $letterValues{$_}; } $digitWords{$digits} = $digitWords{$digits} ? ( $digitWords{$digits} . ", " . $word) : $word; } close IN; while (1) { print "Please input a number or 'q' to quit: "; my $input = <>; chomp($input); last if $input =~ /^q$/i; if ($digitWords{$input}) { print "Words of $input is $digitWords{$input}.\n\n"; } else { print "Sorry, there is no Word for $input.\n\n"; } } exit 0;