#!/usr/bin/perl
print "Content-type: text/html", "\n\n";
print "<html>\n";
print "<body background=\"nevada.gif\">\n";
print "<h2 align=\"center\">Phone Spell</h1>\n";
#
#define stuff to grab data entered on form
#
require CGI;
use CGI;
$cgi = new CGI;
#
# get the email address they entered on the form
#
$fone=$cgi->param('fone');
$case=$cgi->param('case');
$numbers=$cgi->param('numbers');
if ($case eq "" || $case == 0) {
$ucase="";
}
else {
$ucase="CHECKED";
}
if ($numbers == 1) {
$unumbers="checked";
}
else {
$unumbers="";
}
#print "fone=$fone<br>\n";
$fone=~s/"//g;
#print "fone=$fone<br>\n";
print "<center>\n";
print "<form action=\"phone.pl\">";
print "<input type=\"text\" name=\"fone\" size=\"10\" value=\"$fone\"><br>";
print "<input type=\"submit\" name=submit value=\"Spell It\">";
print "<p>";
print "UPPER CASE <INPUT type=\"checkbox\" name=\"case\" value=\"1\" $ucase>";
print "NO Numbers <INPUT type=\"checkbox\" name=\"numbers\" value=\"1\" $unumbers>";
print "</form>";
print "</center>\n";
#if ( $#ARGV == -1 )
# {
# print STDOUT "usage\n";
# print STDOUT "$0 number1 [number2 number3 ...]\n";
# print STDOUT "the program prints out all the spellings of the phone number entered\n";
# print "usage\n";
# print STDOUT "$0 4868\n";
# print "$0 number1 [number2 number3 ...]\n";
# print "the program prints out all the spellings of the phone number entered\n";
# exit 1;
# }
#
# process each number passed to the command line
#
#for $_ (@ARGV)
if ($fone ne "" )
{
# print "processing stupid fone<br>\n";
#$number=$_;
$_=$fone;
$number=$fone;
#
# print the number we are spelling
#
#print "Number<br> $number<br>\n";
#
#if the number contains any letters
#convert them to thecorrect digit
#note Q - becomes 7
#and Z - becomes 9
#and after converting delete anything
#that is not a number
#
# convert output to uppercase
#
if ($case == 1 ) {
print "<div style=\"text-transform: uppercase;\">";
}
if ( ! /^[0-9]+$/ )
{
print "<table border=\"1\" align=\"center\">\n";
print "<tr><td>$number</td>\n";
print "<td> processed as </td>\n";
s/[abc]/2/gi;
s/[def]/3/gi;
s/[ghi]/4/gi;
s/[jkl]/5/gi;
s/[mno]/6/gi;
s/[pqrs]/7/gi;
s/[tuv]/8/gi;
s/[wxyz]/9/gi;
#
#at this point all valid characters have
#been converted to numbers
#delete any characters that are left that
#are not numbers
s/[^0-9]//gi;
#
#tell them what we converted the number to
#
print "<td>$_</td></tr>\n";
print "</table>\n";
}
#
#so we can be lazy convert the number from a number like
# 5467
#to a list of numbers delimited by commas
#and then delete the last comma
s/./$&,/g; # 5467 - 5,4,6,7,
s/,$//; # 5,4,6,7, - 5,4,6,7
#
# convert the numbers into a list or an array
# if we hardcoded it would look like this
# @nums=(5,4,6,7);
#
@nums=split(",");
#
#call the phone speller program which is
#named recur because it recursively calls it self
#
# arg1 - this is the number like 7 which is to be converted
# to letters 7 -> p, r , s
# arg2 - this is the digit number were processing
# 0 is the 1st digit
# 1 is the 2nd digit
# 2 is the 3rd digit and so on
# arg3 - this is the number of digits in the phone number
# were processing minus one.
# C and perl programmers like to think in terms as
# relative to zero instead of relative to one
#print "<br>calling &recur<br>";
print "<tt>";
&recur($nums[0],0,$#nums);
#print "done &recur<br>";
print "</tt>";
}
else
{
#print "Enter a phone number you bozo!<p>";
}
exit 0;
sub recur
{
#print "entering recur<br>";
#
# use my to make these variables dynamic.
# if they are static the program will choke
my $j=0;
my $y;
my $l;
my $start=0;
#
# if the current digit is either zero or one
# only generate one set of letters for it
# because zero and one dont have any letters
# the only way they can be represented is as
# either 1 or 0
#
if ($_[0] <= 1 )
{
$start=2;
}
#
#generate all 3 possible digits for this number
#
#for ($j=0;$j<3;$j++)
#print "recur loop1<br>";
for ($j=$start;$j<3;$j++)
{
#
#call function getdigit to get the next digit
#and stick it on the array
#
$array[$_[1]]=&getdigit($_[0],$j);
#print ($_[1]," ",@array,"\n");
if ($_[1] < $_[2])
{
#
#recursive get the next numbers digit
#
$y=$_[1]+1;
#print "calling recur recursivelly<br>";
&recur($nums[$y],$y,$#nums);
}
else
{
#
# after losts of work we have gotten the last digit of
# this number print it.
#
#print "recur loop2<br>";
for($l=0;$l<=$#array;$l++)
{
print "$array[$l]";
}
print "<br>\n";
#
# end the recursive calls only after the last digit is processed
# these 3 lines are not required, but i have not bothered to
# see if it reduces processing time.
#
if ($j == 2 )
{
#print "returning from recur<br>";
return;
}
}
}
}
sub getdigit
{
#this table defines what
#letters are associated with each number
#some numbers dont have any letters like 0 and 1
#
my @table=((0,0,0),(1,1,1),
("a","b","c"),
("d","e","f"),
("g","h","i"),
("j","k","l"),
("m","n","o"),
("p","r","s"),
("t","u","v"),
("w","x","y")
);
my $num=$_[0]; # grab the telephone number
my $sub=$_[1]; # get the offset letter number
# like for the number 2 if
# the offset is 0 - a is returned
# 1 - b is returned
# 2 - c is returned
my $zsub;
#
#subscript must be 0 to 2 and not 1.4
#
if ($sub < 0 || $sub > 2 || $sub!~/^\d$/ || ! ($sub=~/^[0-9]$/ ))
{
#print STDERR "logic error getdigit needs subscript 0 to 2 we have \"$sub\"\n";
print "logic error getdigit needs subscript 0 to 2 we have \"$sub\"\n";
exit 1;
}
#
# number must be 0 - 9
# nada mas
#
if ($num < 0 || $num > 9 || ! ($num=~/^[0-9]$/ ))
{
#print STDERR "logic error getdigit needs telephone number 0 to 9 we have \"$num\"\n";
print "logic error getdigit needs telephone number 0 to 9 we have \"$num\"\n";
exit 1;
}
$zsub=($num*3)+$sub;
#print ($num," ",$sub," ",($num*3)+$sub," ",$zsub," ",$table[$zsub],"\n");
return $table[$zsub];
}