http://www.perlmonks.org?node_id=216365

mooseboy has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks,

First of all, many thanks to all those who took the time to offer advice on my first Perl program, posted here a week or so ago. I learned a lot from the numerous helpful comments. After some thought, I decided it needed an explanatory table added, and so I humbly present the revised version, and ask once again for the collective wisdom of the Monastery on how it might be improved. I am sure the kludgey way I have created the "notes" hash (just shoving everything into an array and copying it to a hash) must be fairly low down on the list of Ways To Do It. I should also like to ask for enlightenment on how to add three zeroes to the end of each number in the data table and then commify the result.

Thanks once again, mooseboy

#!/usr/bin/perl use diagnostics; use warnings; use strict; use CGI; ## 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, @notes, %notes); while (<DATA>) { ( my $period, $_ ) = split ':'; @{$quotas{$period}}{@members} = split ' '; push @dates, $period; } ## Load notes open (NOTES, 'notes.dat') or die "Couldn't open notes.dat: $!"; my @dates_copy = @dates; while (<NOTES>) { $/ = "\n\n"; my $date = shift @dates_copy; push @notes, ($date, $_); %notes = @notes; } ## Introductory paragraph my $query = CGI->new(); print $query->header("text/html"), $query->start_html( -title => "QuotaBase: a database of OPEC o +il production 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 quotas for that period will be displayed. EOP ) ) ), $query->start_form(), "Choose a period: ", "&nbsp;", $query->popup_menu( -name=>'period', -values=>[@dates], -default=>$dates[0]), "&nbsp;&nbsp;", $query->submit(-name=>'submit', -value=>'Show quotas'), "&nbsp;&nbsp;", $query->defaults('Reset current quotas'), $query->endform; ## Outer table print $query->start_table({ -border => 0, -width => 520 }), $query->start_Tr, $query->start_td; ## 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_td, $query->start_td({-width=>260,-valign=>"top"}); ## Notes table print $query->table({-border=>1,-width=>260}, $query->Tr( $query->td({-align=>"center"},"<b>Note +s</b>") ), $query->Tr( $query->td("<font size=2>$notes{$perio +d}</font>"), ), ); ## End outer table print $query->end_td, $query->end_Tr, $query->end_table; print $query->end_html; __DATA__ Jan 02 - Dec 02: 693 1125 3186 0 1741 1162 1787 562 7053 189 +4 2497 21700 Sep 01 - Dec 01: 741 1203 3406 0 1861 1242 1911 601 7541 202 +5 2670 23201 Apr 01 - Aug 01: 773 1255 3552 0 1941 1296 1993 627 7865 211 +3 2786 24201 Feb 01 - Mar 01: 805 1307 3698 0 2021 1350 2075 653 8189 220 +1 2902 25201

And the 'notes.dat' file looks like this:

As agreed at the 118th Conference, November 14, 2001, and confirmed by + the Consultative Meeting of the Conference, Cairo, Egypt, December 2 +8, 2001. As agreed by the OPEC Conference, July 25, 2001. As agreed at the 114th Conference, March 17, 2001. As agreed at the 113th Conference, January 17, 2001.

Replies are listed 'Best First'.
Re: First program revisited
by chromatic (Archbishop) on Nov 28, 2002 at 18:58 UTC

    Untested, but instead of this:

    my @dates_copy = @dates; while (<NOTES>) { $/ = "\n\n"; my $date = shift @dates_copy; push @notes, ($date, $_); %notes = @notes; }

    ponder this:

    local $/ = "\n\n"; @notes{ @dates } = map { chomp; $_ } <NOTES>;
Re: First program revisited
by kabel (Chaplain) on Nov 28, 2002 at 22:46 UTC
    too tired to answer directly to the code, but for the last two questions see this code:
    use strict; use warnings; my @numbers = 4 .. 6; my $number_of_trailing_zeros = 3; my $commify_at = 3; @numbers = map { $_ .= "0" x $number_of_trailing_zeros; $_ =~ s/(\d{$commify_at})$/,$1/; $_; } @numbers; print "@numbers";
    btw is there a special reason why some write use warnings before use strict?

    anyway, HTH ;)

      Thanks to all for the help. Never really thought about the order of the pragmas, maybe it's the subconscious mind attempting to imitate the order of:

      #!/usr/bin/perl -w use strict;