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;
}

Sort a File Using Sendmail
CGI Online is a service provided by Net Success 2000 Plus.
PO Box 1508
Somerset, KY 42502
Last Modified: October 9, 2007

| HTML TOC | Website Design Workshop | Home |