Ruby Source Code: table.rb
#!/usr/bin/ruby
require 'cgi'
require './common'
#----------------------------------------------------------
# The Web Language Project
# Mark Brautigam
# May-June 2015
# http://www.mixed-up.com/markb/
#----------------------------------------------------------
cgi = CGI.new
puts cgi.header
headers1()
headers2()
print sidebarX("ruby", "table", "rb")
print %q(
<div id="content">
<h2>Ruby: Dynamic Table Data</h2>
)
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);
$state_data_2d = [];
for i in (0.. state_data.length-1)
data = state_data[i].split ("|");
$state_data_2d.push(data);
end
for i in (0.. $state_data_2d.length-1)
$state_data_2d[i][3] = $state_data_2d[i][3].to_i
end
#----------------------------------------------------
def actual_value (s)
#----------------------------------------------------
# This is the only way I could think of :-(
# 500 k -> 500000
# 2.7 m -> 2700000
if (s =~ /m/)
eos = s.index(' ')
n = s[0..eos].to_f
return n * 1000000
elsif (s =~ /k/)
eos = s.index(' ')
n = s[0..eos].to_f
return n * 1000
end
end
#----------------------------------------------------
def draw_main_menu ()
#----------------------------------------------------
print %q(
<p class='choose'>Choose a table:
<a href='table.rb'>Multiplication</a> •
<a href='table.rb?q=ascii'>ASCII</a> •
<a href='table.rb?q=geo'>US States</a>
</p>
)
end
#----------------------------------------------------
def draw_geo (sort)
#----------------------------------------------------
print " <h3>US States, Capitals, and Population</h3>"
draw_main_menu();
print %q(
<p class='choose'>Choose a sort:
<a href='table.rb?q=geo'>Abbreviation</a> •
<a href='table.rb?q=geo&sort=state'>State</a> •
<a href='table.rb?q=geo&sort=pop'>Population</a> •
<a href='table.rb?q=geo&sort=rank'>Rank</a> •
<a href='table.rb?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>
)
if (sort == 'cap')
sorted = $state_data_2d.sort { |a,b| a[4] <=> b[4] }
elsif (sort == 'state')
sorted = $state_data_2d.sort { |a,b| a[1] <=> b[1] }
elsif (sort == 'rank')
sorted = $state_data_2d.sort { |a,b| a[3] - b[3] }
elsif (sort == 'pop')
sorted = $state_data_2d.sort { |a,b| actual_value(a[2]) - actual_value(b[2]) }
else
sorted = $state_data_2d.sort { |a,b| a[0] <=> b[0] }
end
for i in (0.. sorted.length-1)
print "<tr>";
for j in (0..sorted[i].length-1)
print "<td>" + sorted[i][j].to_s + "</td>"
end
print "</tr>\n";
end
print "</table>\n";
end
#----------------------------------------------------
def draw_mult
#----------------------------------------------------
print " <h3>Multiplication Table</h3>"
draw_main_menu()
print %q(
<table class='mult'>
<tr><th></th>
)
for i in 1..12
print "<th>" + i.to_s + "</th>"
end
print "</tr>\n"
for r in 1..12
print "<tr><th>" + r.to_s + "</th>"
for c in 1..12
p = r*c;
print "<td>" + p.to_s + "</td>"
end
print "</tr>\n"
end
print "</table>"
end
#----------------------------------------------------
def draw_ascii ()
#----------------------------------------------------
print " <h3>ASCII Table</h3>"
draw_main_menu()
print %q(
<table class='mult ascii'>
<tr><th></th>
)
for i in 0..15
print ("<th>" + i.to_s(16) + "</th>")
end
print ("</tr>\n");
for r in 2..7
print ("<tr><th>" + r.to_s + "</th>")
for c in 0..15
p = 16*r+c;
print ("<td>" + p.chr + "</td>")
end
print ("</tr>\n");
end
print "</table>\n"
end
#----------------------------------------------------
# main section
#----------------------------------------------------
if (cgi['q'] == 'ascii')
draw_ascii()
elsif (cgi['q'] == 'geo')
draw_geo(cgi['sort'])
else
draw_mult()
end
print %q(
<p><a href='source.rb?f=6'>Show Ruby source code »</a>
</div>
)
footers()