IHYPRESS PROGRAMMING
Tutorials and Perl programs with code and output for beginners
Perl logo
HOME | ASP | C | CSS | GNUPLOT | HTML | JAVASCRIPT | PERL | PHP | PYTHON | RUBY | SVG
Perl ❯ Sorting Arrays
<07>
#!/usr/bin/perl -wT use CGI':standard'; use CGI::Carp qw(warningsToBrowser fatalsToBrowser); my @cities; my @numbers; my $i; my $array_length; @cities = ("Albuquerque ", "Phoenix ", "Tucson ", "Sacramento ", "Flagstaff ", "Sedona ", "Amarillo "); @numbers = (209, 215, 2119, 2901, 1404, 1407, 792, 113, 1010); print "Content-type: text/html\n\n"; print "<!DOCTYPE html>"; print "<html><head><title>Sorting Arrays</title></head>"; print "<body>"; print "<div style='font-size:1.2em;'>Original Array:<br>"; print @cities; print "</div>"; @cities = reverse (@cities); print "<p style='font-size:1.2em;'>Reversed Array:<br>"; print @cities; print "</p>"; @cities = sort (@cities); print "<p style='font-size:1.2em;'>Sorted Array:<br>"; print @cities; print "</p>"; @cities = sort{$b cmp $a} (@cities); print "<p style='font-size:1.2em;'>Descending Sorted Array:<br>"; print @cities; print "</p>"; $array_length = @numbers; print "<p style='font-size:1.2em;'>Original:<br>"; for ($i = 0; $i < $array_length; ++$i){ print $numbers[$i];print " "; } print "</p>"; #ascii sort @numbers = sort (@numbers); print "<p style='font-size:1.2em;'>Sorted (ASCII):<br>"; for ($i = 0; $i < $array_length; ++$i){ print $numbers[$i];print " "; } print "</p>"; #numeric sort @numbers = sort {$a <=> $b} (@numbers); print "<p style='font-size:1.2em;'>Sorted (Numeric):<br>"; for ($i = 0; $i < $array_length; ++$i){ print $numbers[$i];print " "; } print "</p>"; print "</body></html>";
Hergestellt in Deutschland / Made in Germany
Sorting Arrays
Original Array:
Albuquerque Phoenix Tucson Sacramento Flagstaff Sedona Amarillo

Reversed Array:
Amarillo Sedona Flagstaff Sacramento Tucson Phoenix Albuquerque

Sorted Array:
Albuquerque Amarillo Flagstaff Phoenix Sacramento Sedona Tucson

Descending Sorted Array:
Tucson Sedona Sacramento Phoenix Flagstaff Amarillo Albuquerque

Original:
209 215 2119 2901 1404 1407 792 113 1010

Sorted (ASCII):
1010 113 1404 1407 209 2119 215 2901 792

Sorted (Numeric):
113 209 215 792 1010 1404 1407 2119 2901

COPYRIGHT © 2015-2024 IHY PRESS Frankfurt am Main 60329 Deutschland