Free Web Hosting by Netfirms
Web Hosting by Netfirms | Free Domain Names by Netfirms

Free Web Hosting by Netfirms
Web Hosting by Netfirms | Free Domain Names by Netfirms

Also: View my free sample code in Visual Basic - Free Macros for MS Word

The following is PERL code. It is the complete code for a "bare-bones" version of my IEP Translation Dictionary. The version below is fully operational as long as you use it in conjunction with a correctly-formatted text file containing the dictionary contents. Here, the text file is referred to as "notcgi.cgi" However, it could be named any other name (mydictionary.txt, for example) as long as all references to "notcgi.cgi" in the program are changed accordingly. You may cut and paste the perl code from this page and use it to run your own search program on your own server if you wish. Thank you for your interest!

#!/usr/local/bin/perl

use CGI qw(:cgi-lib);
&ReadParse;

## The following "if" statement will check to make sure
## that a word was entered for the dictionary search.
## The original HTML page contained a text input named "w1"
## Therefore, we look in the input hash ($in{___})
## to see if it contains input for "w1"
## If there is no input, then the program prints out an
## HTML page with information for the user and then the
## program terminates

if (!$in{'w1'}){
print "Content-type: text/html\n\n";

print "<\!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN";
print "\"\"http://www.w3.org/TR/html4/loose.dtd\">";
print "<HTML><HEAD><meta http-equiv=\"Content-Type\" content=";
print "\"text/html\; charset=utf-8\">";
print "<TITLE>IEP Translation Dictionary</TITLE></HEAD><BODY>";

print "No input was entered. Please hit the \"Back\" button and make ";
print "sure to enter a word or partial word before attempting a search.";
print "</body></html>";

exit;
}

#######################################################

## Open the file named "notcgi.cgi" and store all of
## the file's data in an array named "@raw_data"
## Everytime the file contains a carriage return,
## (carriage return, aka new line, or return character)
## the perl language will automatically add a new
## element to @raw_data. So, for example, if the
## raw data file contains 75 lines, then the array
## will contain 75 elements.

open(DIC,"notcgi.cgi");
@raw_data=<DIC>;
close(DIC);

#######################################################

## The following subroutine, called "noaccents" will
## be used later in the program. I will later call
## the subrouting by writing "&noaccents(_____)"
## The subroutine will take one string variable
## and replace any accented vowel characters with
## their unaccented counterpart. This serves a
## valuable purpose in the dictionary search, because
## if the user wants to search for "límite," then
## i want them to be able to enter "límite" OR
## "limite" (without the accent on the i) and still
## get the same results

sub noaccents {
my($word) = @_[0];

## Note: the array of input sent to the subroutine is
## automatically named "@_" by perl

%accents = qw(á a é e í i ó o ú u ü u &ntilde; n ñ n Á a É e Í i Ó o Ú u Ü u &Ntilde; n Ñ n);

## %accents is a hash containing the characters to
## be replaced followed by their replacement

for $key ( keys %accents ) {
$word=~ s/$key/$accents{$key}/g;

## a regex to replace any of áéíóú with aeiou

}
return($word);
}

#######################################################

## The following subroutine will take a string and
## replace the 'non-displaying' special characters
## (that is, characters that don't display in HTML
## pages, such as ¡¿Ñ) with their correct
## HTML reference, such as &iexcl;
## It is important to note that the subroutine
## has two steps. The first step is to make
## sure any ampersands in the original text will
## display as intended. However, this MUST be
## done before the second step, because the second
## step will insert references that contain
## ampersands.

sub HTMLproof {
my($word) = @_[0];

## Note: the array of input sent to the subroutine is
## automatically named "@_" by perl

%badcharacters1 = qw(& &amp;);

for $key ( keys %badcharacters1 ) {
$word=~ s/$key/$badcharacters1{$key}/g;

## See above notes inside the "noaccents" subroutine

}

%badcharacters2 = qw(" &quot; ¡ &iexcl; « &laquo; » &raquo; ¿ &iquest; Á &Aacute; É &Eacute; Í &Iacute; Ñ &Ntilde; Ó &Oacute; Ú &Uacute; Ü &Uuml; á &aacute; é &eacute; í &iacute; ñ &ntilde; ó &oacute; ú &uacute; ü &uuml;);

## %badcharacters2 is a hash containing the characters to
## be replaced followed by their replacement

for $key ( keys %badcharacters2 ) {
$word=~ s/$key/$badcharacters2{$key}/g;
}
return($word);
}

#######################################################

## The following loop, "for ($cnt1=0; $cnt1<@raw_data; $cnt1++)"
## will cycle through all of the data in the dictionary
## file that was opened at the beginning of the program.
## Each line of the dictionary file contains both
## the English and the Spanish version of the same
## term. Therefore, if the SEARCH ENTRY matches any
## part of the line, then we will want to display
## the whole line, because in this way we will display
## the search term and its translation.

$found=0;

for ($cnt1=0; $cnt1<@raw_data; $cnt1++)
{
if (index( lc(&noaccents(@raw_data[$cnt1])),lc( &noaccents($in{'w1'}) ))!=-1)
{
$newaddition = @raw_data[$cnt1];

#### beginning of DO LOOP ####

## Each time the FOR loop finds a line to add to the
## results, the following "DO" loop will find the
## exact position in the line where the search text
## is located. Then, it will add the
## appropriate HTML code to the search results to
## achieve a bold highlighted effect on the text
## that matched the search.

$posicion = 0;
do {
$where = index(lc(&noaccents($newaddition)),lc( &noaccents($in{'w1'}) ),$posicion);
$mylength = length(&noaccents($in{'w1'}) );

$newaddition=~ s/^(.{$where})(.{$mylength})/\1\<\B\>\<FONT SIZE\=\+1\>\2\<\/FONT\>\<\/\B\>/;

## Note: the preceding line is a regex that finds the
## beginning and end positions of the search entry
## within the dictionary line, and then adds the
## HTML bold and large font effects on either side
## of the search entry match.

$posicion = $where + $mylength + 27;

} while ( index(lc(&noaccents($newaddition)),lc( &noaccents($in{'w1'}) ),$posicion)!=-1 );

#### end of DO ########################################

$whatifound =$whatifound . "\^" . $newaddition; $found=1;

## (Above) I am inserting "^" characters to mark where
## the end of each line was as I join all of the
## search results together in one variable, which is
## the variable "$whatifound"

}
}

#######################################################

$whatifound = &HTMLproof($whatifound);

## (above) "HTML-proof" the entire string of results

## Print the header of the HTML page that will display
## the results

print "Content-type: text/html\n\n";

print "<\!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\"http://www.w3.org/TR/html4/loose.dtd\">";
print "<HTML><HEAD><meta http-equiv=\"Content-Type\" content=\"text/html\; charset=utf-8\">";
print "<TITLE>IEP Translation Dictionary</TITLE>\<STYLE TYPE \= \"text\/css\"\>.menulink1\:link ";
print "\{ color\: \#FFCC00\; text\-decoration\: none\}.menulink1\:visited \{ color\: \#FFCC00\; ";
print "text\-decoration\: none\}.menulink1\:hover \{ color\: #66FF99\; text\-decoration\: none\}.menu ";
print "\{ color\: \#FFFFFF\; font\-family\: Arial\; font-size\: 14px\; fontsize\: 15px\; font-weight\: bold\}";
print ".w3ctable\:link \{ color\: \#0033CC\; text-decoration\: underline\}.w3ctable\:visited ";
print "\{ color\: \#0033CC\; text-decoration\: underline\}.w3ctable\:hover \{ color\: \#0033CC\; ";
print "text-decoration\: underline\}\<\/STYLE\>\<\/HEAD\>\<BODY\>";

print "<P\>\<CENTER\>\<TABLE BGCOLOR\=\"\#336699\" CELLSPACING\=0 WIDTH\=750 STYLE\=\"background\-image\:url\(\'..\/images\/navbar.gif\'\)\;\"\> \<TR\>\<TD WIDTH\=\"100\%\" HEIGHT\=\"24\" VALIGN\=\"middle\" ALIGN\=\"center\"\>\<DIV CLASS\=\"menu\"\>\<A HREF\=\"..\/index.htm\" CLASS\=\"menulink1\"\>HOME\<\/A\>\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\; \<A HREF\=\"..\/dictionary.html\" CLASS\=\"menulink1\"\>TRANSLATION\<\/A\>\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\; \<A HREF\=\"..\/principal1\/webtools.shtml\" CLASS\=\"menulink1\"\>WEB TOOLS\<\/A\>\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\; \<A HREF\=\"..\/principal1\/perl_tools.shtml\" CLASS\=\"menulink1\"\>PERL\<\/A\>\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\; \<A HREF\=\"..\/principal1\/misc.shtml\" CLASS\=\"menulink1\"\>MISC\<\/A\>\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\; \<A HREF\=\"..\/principal1\/kelly_k_jones.shtml\" CLASS\=\"menulink1\"\>MY RESUME\<\/A\>\<\/DIV\>\<\/TD\>\<\/TR\>\<\/TABLE\>\<\/CENTER\>";

#######################################################

if ($found==0)
{
print "<P>not found";
}
else
{
print "<P>I did find your word";

print "<TABLE BORDER=1>";

while (index($whatifound, "\^")!=-1){

## The following lines cycle through four different
## pre-determined colors so that each row of
## the HTML table will display in a color that
## distinguishes it from the rows above and below

$cnt3 = $cnt3 + 1;
$color= $cnt3 / 4;

if (index($color, ".")!=-1){
$color= substr($color, index($color, ".") + 1);}
else{$color=0;}

%bgcolors = qw(25 99CCFF 5 CCCCFF 75 FFCCFF 0 FFCCCC);

#### End of the color selection process #########

## The following regexes will find the "^"s that I put
## into "$whatifound" earlier to mark where lines should
## end/begin. When a "^" is found, it is replaced with
## the HTML code necessary to begin a new table row.
## Then the other regex will look for "|"
## The "|" characters were part of the original raw
## data file. In the raw data file, each line
## contains the English word, then "|", then the
## Spanish definition.

$whatifound=~ s/\^/\<TR\>\<TD BGCOLOR\=\"\#$bgcolors{$color}\"\>/;
$whatifound=~ s/\|/\<TD BGCOLOR\=\"\#$bgcolors{$color}\"\>/;

}

print "<P>$whatifound";

print "</TABLE>";

}

#######################################################

## Print the footer of the HTML page

print "<P>";

print "<P\>\<CENTER\>\<TABLE BGCOLOR\=\"\#336699\" CELLSPACING\=0 ";
print "WIDTH\=750 STYLE\=\"background\-image\:url\(\'..\/images\/navbar.gif\'\)\;";
print "\"\> \<TR\>\<TD WIDTH\=\"100\%\" HEIGHT\=\"24\" VALIGN\=\"middle\" ";
print "ALIGN\=\"center\"\>\<DIV CLASS\=\"menu\"\>\<A HREF\=\"..\/index.htm\" ";
print "CLASS\=\"menulink1\"\>HOME\<\/A\>\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;";
print "\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\; \<A HREF\=\"..\/dictionary.html\" ";
print "CLASS\=\"menulink1\"\>TRANSLATION\<\/A\>\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;";
print "\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\; \<A HREF\=\"..\/principal1";
print "\/webtools.html\" CLASS\=\"menulink1\"\>WEB TOOLS\<\/A\>\&nbsp\;\&nbsp\;\&nbsp\;";
print "\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\; \<A HREF\=\"..\/principal1";
print "\/perl_tools.html\" CLASS\=\"menulink1\"\>PERL\<\/A\>\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;";
print "\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\; \<A HREF\=\"..\/principal1\/misc.html\" ";
print "CLASS\=\"menulink1\"\>MISC\<\/A\>\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\;";
print "\&nbsp\;\&nbsp\;\&nbsp\;\&nbsp\; \<A HREF\=\"..\/principal1\/kelly_k_jones.html\" ";
print "CLASS\=\"menulink1\"\>MY RESUME\<\/A\>\<\/DIV\>\<\/TD\>\<\/TR\>\<\/TABLE\>\<\/CENTER\>";

print "</BODY></HTML>";

########## End of Program #############################