Perl Source Code: table.pl
#!/usr/bin/perl
use CGI qw(:standard);
use common;
#----------------------------------------------------------
# The Web Language Project
# Mark Brautigam
# May-June 2015
# http://www.mixed-up.com/markb/
#----------------------------------------------------------
myUtils::headers1();
myUtils::headers2();
print myUtils::sidebar ("perl", "table", "pl");
print (" <div id='content'>\n");
print (" <h2>Perl: Dynamic Table Data</h2>\n");
#--------------------------------------------------
# The main Perl code is here.
# Everything else is boilerplate.
#--------------------------------------------------
@state_data = (
"NY|New York|19 m|3|Albany",
"NJ|New Jersey|8.7 m|10|Trenton",
"VT|Vermont|620 k|49|Montpelier",
"IN|Indiana|6.2 m|15|Indianapolis",
"OH|Ohio|11 m|7|Columbus",
"TX|Texas|22 m|2|Austin",
"NM|New Mexico|1.9 m|36|Santa Fe",
"MA|Massachusetts|6.3 m|13|Boston",
"CO|Colorado|4.6 m|22|Denver",
"TN|Tennessee|5.9 m|16|Nashville",
"ND|North Dakota|630 k|48|Bismarck",
"WY|Wyoming|500 k|50|Cheyenne",
"OK|Oklahoma|3.5 m|29|Oklahoma City",
"WV|West Virginia|1.8 m|37|Charleston",
"NV|Nevada|2.4 m|35|Carson City",
"PA|Pennsylvania|12 m|6|Harrisburg",
"CA|California|36 m|1|Sacramento",
"WA|Washington|6.2 m|14|Olympia",
"GA|Georgia|9.0 m|9|Atlanta",
"MO|Missouri|5.8 m|18|Jefferson City",
"NH|New Hampshire|1.3 m|41|Concord",
"HI|Hawaii|1.2 m|42|Honolulu",
"MD|Maryland|5.6 m|19|Annapolis",
"DE|Delaware|840 k|45|Dover",
"AK|Alaska|660 k|47|Juneau",
"MN|Minnesota|5.1 m|21|Saint Paul",
"NC|North Carolina|8.6 m|11|Raleigh",
"KY|Kentucky|4.1 m|26|Frankfort",
"MT|Montana|930 k|44|Helena",
"LA|Louisiana|4.5 m|24|Baton Rouge",
"NE|Nebraska|1.7 m|38|Lincoln",
"WI|Wisconsin|5.5 m|20|Madison",
"AL|Alabama|4.5 m|23|Montgomery",
"IA|Iowa|2.9 m|30|Des Moines",
"VA|Virginia|7.5 m|12|Richmond",
"ME|Maine|1.3 m|40|Augusta",
"ID|Idaho|1.4 m|39|Boise",
"AR|Arkansas|2.7 m|32|Little Rock",
"UT|Utah|2.4 m|34|Salt Lake City",
"MS|Mississippi|2.9 m|31|Jackson",
"RI|Rhode Island|1.0 m|43|Providence",
"FL|Florida|17 m|4|Tallahassee",
"OR|Oregon|3.6 m|27|Salem",
"SD|South Dakota|770 k|46|Pierre",
"MI|Michigan|10 m|8|Lansing",
"KS|Kansas|2.7 m|33|Topeka",
"CT|Connecticut|3.5 m|28|Hartford",
"SC|South Carolina|4.2 m|25|Columbia",
"IL|Illinois|12 m|5|Springfield",
"AZ|Arizona|5.9 m|17|Phoenix" );
#print (@state_data);
#print ($state_data[1]);
#-------------------------------------------------------------
sub init_2d
#-------------------------------------------------------------
{
@$state_data_2d = ();
for ($i=0, $n=@state_data; $i<$n; $i++) {
@data = split ("\\|", $state_data[$i]);
# http://www.perlmonks.org/?node_id=166572
# pushing multi-dimensional arrays onto each other
push (@state_data_2d, [@data]);
}
}
#-------------------------------------------------------------
if (param("q") eq 'ascii') {
draw_ascii();
}
elsif (param("q") eq 'geo') {
draw_geo(param("sort"));
}
else {
draw_mult();
}
#-------------------------------------------------------------
sub popsort
#-------------------------------------------------------------
{
#if (strchr($a->[2], 'm') > -1 && strchr($b->[2], 'k') > -1) { #/ a bigger
if ($a->[2] =~ /m/ && $b->[2] =~ /k/) {
return 1;
}
#if (strchr($a->[2], 'k') > -1 && strchr($b->[2], 'm') > -1) { #/ a bigger
if ($a->[2] =~ /k/ && $b->[2] =~ /m/) {
return -1;
}
#return floatval($b[3]) - floatval($a[3]);
return $b->[3] cmp $a->[3];
}
#-------------------------------------------------------------
sub draw_table_menu
#-------------------------------------------------------------
{
print <<mainmenu;
<p class='choose'>Choose a table:
<a href='table.pl'>Multiplication</a> •
<a href='table.pl?q=ascii'>ASCII</a> •
<a href='table.pl?q=geo'>US States</a>
</p>
mainmenu
}
#-------------------------------------------------------------
sub draw_geo
#-------------------------------------------------------------
{
$sort = $_[0];
print $sort;
init_2d();
# print $state_data_2d[1][1];
if ($sort eq 'state') {
@sorted = sort { $a->[1] cmp $b->[1] } @state_data_2d;
}
elsif ($sort eq 'cap') {
@sorted = sort { $a->[4] cmp $b->[4] } @state_data_2d;
}
elsif ($sort eq 'rank') {
@sorted = sort { $a->[3] <=> $b->[3] } @state_data_2d;
}
elsif ($sort eq 'pop') {
# @sorted = sort { $a->[2] <=> $b->[2] } @state_data_2d;
@sorted = sort popsort @state_data_2d;
}
else {
@sorted = sort { $a->[0] cmp $b->[0] } @state_data_2d;
}
print "<h3>US States, Capitals, and Population</h3>\n";
draw_table_menu();
print <<geoheader;
<p class='choose'>Choose a sort:
<a href='table.pl?q=geo'>Abbreviation</a> •
<a href='table.pl?q=geo&sort=state'>State</a> •
<a href='table.pl?q=geo&sort=pop'>Population</a> •
<a href='table.pl?q=geo&sort=rank'>Rank</a> •
<a href='table.pl?q=geo&sort=cap'>Capital</a>
</p>
<table class='results geo'>
<tr><th>Abbr</th><th>State</th><th>Population</th><th>Rank</th><th>Capital</th></tr>
geoheader
my $i, $j, $n, $d;
for ($i=0, $n=@sorted; $i<$n; $i++) {
print "<tr>";
# a fudge since I can't count the number of elements in sub-array
for ($j=0; $j<5; $j++) {
print "<td>$sorted[$i][$j]</td>";
}
print "</tr>\n";
}
print "</table>\n";
}
#-------------------------------------------------------------
sub draw_ascii
#-------------------------------------------------------------
{
my $i, $r, $c, $p, $ascii;
print "<h3>ASCII Table</h3>\n";
draw_table_menu();
print "<table class='mult ascii'>\n";
print "<tr><th></th>\n";
for ($i=0; $i<16; $i++) {
printf ("<th>%X</th>", $i);
}
print "</tr>\n";
for ($r=2; $r<8; $r++) {
print "<tr><th>$r</th>";
for ($c=0; $c<16; $c++) {
$ascii = chr (16*$r+$c);
print "<td>$ascii</td>";
}
print "</tr>\n";
}
print "</table>\n";
}
#-------------------------------------------------------------
sub draw_mult
#-------------------------------------------------------------
{
my $i, $r, $c, $p;
print "<h3>Multiplication Table</h3>\n";
draw_table_menu();
print " <table class='mult'>\n";
print " <tr><th></th>\n";
for ($i=1; $i<=12; $i++) {
print ("<th>$i</th>");
}
print ("</tr>\n");
for ($r=1; $r<=12; $r++) {
print ("<tr><th>$r</th>");
for ($c=1; $c<=12; $c++) {
$p = $r*$c;
print ("<td>$p</td>");
}
print ("</tr>\n");
}
print ("</table>\n");
}
#--------------------------------------------------
print <<foot;
<p><a href='source.pl?f=6'>Show Perl source code »</a>
</div>
foot
myUtils::footers();