PERL CGI Tutorial for Writing Interactive Form Scripts
Working With Numbers
An array (list) of numbers could be defined in PERL with the following line of code:
@array= (1,2,3,4,5,6,7,8,9);
When sorting numbers a different routine is needed than the one presented in the previous lesson. We'll make use of a subroutine shown below and add the code to access the subroutine.
sub byNUM {
return $a <=>$b;
}
The numbers in the array shown below would remain in the same order if sorted using the simple ascii bubble sort.
@array = (1,12,2,23,3,34,4,43);
The code to access the sub for sorting in proper order is:
@array=(1,12,2,23,3,34,4,43);
foreach $i (sort byNUM @array) {
print "$i\n";
}
math_examples.pl
This CGI script demontrates the use of a subroutine.
#!/usr/bin/perl
print "Content-type:text/html\n\n";
print "<html>\n";
print "<head><title></title></head>\n";
print "<body>\n";
print "Original order: 1,12,2,23,3,34,4,43 Sorted: \n";
@array=(1,12,2,23,3,34,4,43);
foreach $i (sort byNUM @array) {
print "$i\n";
}
print "</body>\n";
print "</html>\n";
sub byNUM {
return $a <=> $b;
}
|
Contents
Getting Started in CGI
Send HTML Page to Browser
Parse HTML Form
Saving to a File
Open a File and Display Contents
Using the Split Function
Search a File
Sort a File
Display a Multi Line File in Tables
Display a Multi Line File in a Textarea
Working with Numbers
Using Sendmail
|