I'm writing a web based calander, and want to display a month (or more) in a html table, add links and pull out whatever data is associated with the link (ala the yahoo datebook).

Now i've got it printing out a nice little table with all the dates in the correct spots etc, (which is good) but i'm iterating thru' three loops. one to pull out the days, one to build a monthly matrix (much like the `cal` command), and the final loop builds the html.

I'm thinking this is pretty slow and inefficient, and was wondering if there is a better way.

I thought of using a hash for the number of days in a month (and making the appropriate exceptions for a LY, then determining what day is the 1st of the month) however i'm still stuck with two loops.

Naturally any comments on the style of the code appreciated.

#!/usr/bin/perl -w use strict; use Data::Dumper; use Date::Calc qw( Delta_Days Add_Delta_Days Date_to_Text ); use CGI; my $q = new CGI; sub date_range { my(@date) = (@_)[0,1,2]; my(@list); my($i); my $output = ''; my $date; my %pos = (Su=>1, Mo=>2, Tu=>3, We=>4, Th=>5, Fr=>6, Sa=>7 ); my @box; $i = Delta_Days(@_); while ($i-- >= 0) { push( @list, [ @date ] ); @date = Add_Delta_Days(@date, 1) if ($i >= 0); } #build the matrix to hold the days. my @box; #the arrray to hold the calendar data my $row=0; foreach $date (@list) { my $col = $pos{substr(Date_to_Text(@{$date}),0,2)}; $box[$row][$col]= (@{$date}[2]); if ($col eq 7) {$row++} } for (my $i=0; $i<5; $i++){ $output .= "<tr>"; for (my $j=0; $j<8;$j++){ if (! defined $box[$i][$j]) { $output .= $q->td({align=>"right"},"&nbsp"); } else { $output .= $q->td({align=>"right"},$box[$i][$j]); } } $output .= "</tr>"; } return $output; } print $q->header(); print $q->table(&date_range(2001,11,1, 2001,11,30)); # in chronologica +l order

In reply to Web month Calander by Ryszard

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.