#!/usr/local/bin/perl5 -w
#  Simple sort function Example
#
require "cgi-lib.pl"; # Public Domain library to read form data
#
$statusofread =  &ReadParse(*formdata); # read data in associate array formdata
#
if( $statusofread )     { # If $statusofread nonzero, data read OK
        $arraystring = $formdata{"array"}; # The input array with comma separators
        $method = $formdata{"sortmethod"}; # The input sort method
	@array = split(/\s*,\s*/,$arraystring); # Use split to get array
}
#
$num = @array; # Number of entries in @array
if( $method eq 'backsortchar' )	{
	@result = sort backsortchar @array; # sort
}
elsif( $method eq 'backsortnum' )	{
	@result = sort backsortnum @array; # sort
}
elsif( $method eq 'forwsortnum' )	{
	@result = sort forwsortnum @array; # sort
}
else	{ # default
	@result = sort @array; # sort forward stringwise
}
$today =`date`;
chomp($today); # Today's date captured from UNIX
#
print <<EOF; # Generate the HTML responding page starting with HTTP line
Content-Type: text/html

<html>
<head>
<title>This is Result of my sort CGI Script </title>
</head>
<body bgcolor="white" >
<h1>Result of Sort with method $method on a great $today</h1>
<font size=7 >
Number of Entries $num<br>
Original: @array<br>
Sorted: @result<br>
</font>
</body>
</html>
EOF
exit; # End Program
#
sub backsortchar { $b cmp $a; } # Backward Characters
sub backsortnum { $b <=> $a; } # Backward Numbers
sub forwsortnum { $a <=> $b; } # Forward Numbers
