Wow, many thanks to all who responded so far! I'll have to spend a while to try and synthesize all the thoughtful suggestions and see if I can come up with an improved version.
Cheers, mooseboy
| [reply] [Watch: Dir/Any] |
Chmrr, many, many thanks for your "native Perl" rewrite of my effort! One question: in your version, the dates in the pop-up menu are not ordered -- is there a way to order them so that the most recent is first, then the next most recent, etc, as in the original?
<Thanks, mooseboy
| [reply] [Watch: Dir/Any] |
Not to sound cliche or anything, but you must first understand the cause of the problem; then the solution will become obvious. The reason the popup ordering isn't in order is because it is gotten through keys %some_hash -- which, as we know, produces said keys in no (easily) predictable or useful order. While it is possible that one could attempt to re-sort the list that it returns into some useful order, the current date format looks to make that rather hard.
Given that, what we really want is to keep track of the keys of the hash as we add them, in some structure which preserves order -- say, like an array or something..
Thus, we do is have an @dates array (declared next to where we define %quotas, say), and push $period onto the end of @dates as we go though loading the data. Later on, we can use @dates instead of keys %quotas if the ordering matters.
I've intentionally not provided much explicit code here. Consider it an "excercise left to the reader." ;>
perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'
| [reply] [Watch: Dir/Any] [d/l] [select] |
| [reply] [Watch: Dir/Any] |
#!/usr/bin/perl
use warnings;
use strict;
use CGI;
## Chmrr's revision, very slightly modified
## Column headings
my @members = qw/ Algeria Indonesia Iran Iraq Kuwait Libya Nigeria Qat
+ar Saudi_Arabia UAE Venezuela total_OPEC /;
@members = map { tr/_/ /; $_ } @members;
## Load data
my (%quotas, @dates);
while (<DATA>) {
( my $period, $_ ) = split ':';
push @dates, $period;
@{$quotas{$period}}{@members} = split ' ';
}
## Introductory paragraph
my $query = CGI->new();
print $query->header("text/html"),
$query->start_html( -title => "QuotaBase: a database of OPEC oil p
+roduction quotas",
-bgcolor => "#cbcbcb" ),
$query->h1("Welcome to QuotaBase!"),
$query->table(
$query->Tr(
$query->td({-width => "600"},
$query->p(<<EOP)
QuotaBase is an interactive database of OPEC oil production quotas. By
+ default, the <b>current quotas</b> are displayed. To view <b>histori
+cal quota information</b>, select the period you want from the drop-d
+own list and click the 'Show quotas' button. A table of all the quota
+s for that period will be displayed.
EOP
)
)
),
$query->start_form(),
"Choose a period: ",
" ",
$query->popup_menu( -name=>'period',
-values=>[@dates],
-default=>(@dates)[0]),
" ",
$query->submit(-name=>'submit', -value=>'Show quotas'),
" ",
$query->defaults('Reset current quotas'),
$query->endform;
## Data table
my $period = $query->param('period') || (@dates)[0];
print $query->table({-border=>1},
$query->Tr(
$query->td({-colspan=>2,-align=>"center
+"},"<b>$period</b>")
),
map {
$query->Tr(
$query->td({-width=>130,-align=>"left
+" }, $_),
$query->td({-width=>130,-align=>"righ
+t"}, $quotas{$period}{$_})
)
} sort keys %{$quotas{$period}}
);
print $query->end_html;
__DATA__
Jan 02 - Dec 02: 693 1125 3186 0 1741 1162 1787 562 7053 1894 2
+497 21700
Sep 01 - Dec 01: 741 1203 3406 0 1861 1242 1911 601 7541 2025 2
+670 23201
Apr 01 - Aug 01: 773 1255 3552 0 1941 1296 1993 627 7865 2113 2
+786 24201
Feb 01 - Mar 01: 805 1307 3698 0 2021 1350 2075 653 8189 2201 2
+902 25201
31 Oct 00 - Jan 01: 853 1385 3917 0 2141 1431 2198 692 8674 2333 3
+077 26700
1 Oct 00 - 30 Oct 00: 837 1359 3844 0 2101 1404 2157 679 8512 2289 3
+019 26200
| [reply] [Watch: Dir/Any] [d/l] |