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

Hi,

In the following script, the values of @Calendar are printed properly as required. However the commented out array has a sequence of dates which cause a failure of the script.

It is not the sequence - any one of the dates in the sequence if placed in the uncommented array causes failure.

I can't see what the problem is, and seek help on these friendly shores

#!/usr/bin/perl -w use strict; use CGI qw(:standard); use Date::Calc qw(:all); use HTML::AsSubs; use HTML::Element; use HTML::CalendarMonth; use Data::Dumper; my @Calendar = qw~732281 732282 732283 732284 732285 732286 732287 732 +288 732289 732290 732239 732240 732241 732242 732243 732244 732245 73 +2246 732247 732401 732402 732403 732404 732405 732406 732407 732408 7 +32409 732410 732411 732412 732413 732414 732415 732451 732452 732453 +732454 732455 732456 732457 732458 732459 732460 732461 732462 732463 + 732464 ~; #@Calendar = qw~732661 732662 732663 732664 732665 732666 732667 73266 +8 732669 732670 732671 732672 732673 732674 732675 732676~; my @dates = sort @Calendar; my ( $param_year, %cal, $day_first, $day_last, @color, %cal, @cals, $line ); print header(), start_html(-title => "Calendars"); my ($year1, $month1, $day1) = Add_Delta_Days(1, 1, 1, $dates[0] - 1); my ($year2, $month2, $day2) = Add_Delta_Days(1, 1, 1, $dates[$#dates] +- 1); #uncomment lines below if you want to start calendar from current mont +h (undef,undef,undef,undef,$month1)=localtime(); $month1++; if (!defined($param_year)) { $param_year=$year1;} foreach($year1..$year2) { $cal{$_}=[]; } #print Dumper(\%cal); my $year=$year1; while($year<$year2 || $month1<=$month2) { my $new_c = new HTML::CalendarMonth(year => $year, month => $month1) +; my $result = $new_c->last_row; unless ($result == 7) { $new_c->maxrow($new_c->maxrow + 1); } $day_first=Date_to_Days($year,$month1,1); $day_last=$day_first + Days_in_Month($year,$month1)-1; #search days of current month @color = map { get_day($_) } grep { $_>=$day_first && $_<=$day_last +} @dates; $new_c->item($new_c->month, $new_c->year)->wrap_content( font({ size + => '2' }) ); $new_c->item($new_c->dayheaders)->wrap_content( font({ size => '2' } +) ); if(@color) { $new_c->item( @color )->wrap_content( strike( font({ size => 3}) )) +; $new_c->item( @color )->attr(bgcolor => 'red'); } push(@{$cal{$year}}, $new_c); if ($month1<12) {$month1++;} else {$month1=1;$year++;} } my(undef,undef,undef,undef,undef,$curyear)=localtime(); $curyear+=1900; foreach $year($year1..$year2) { @cals=@{$cal{$year}}; # Generate container table my $t = new HTML::ElementTable(maxrow => int($#cals/4), maxcol => ($ +#cals<4)?$#cals:3); #$t->attr('border',1); # Populate container table for my $calnum (0..$#cals) { my $col = $calnum %4; my $row = int($calnum / 4); $t->cell($row, $col)->push_content($cals[$calnum]); } $line=$t->as_HTML; print $line; } print end_html(); exit(0); sub get_day($) { my(undef,undef,$day)=Add_Delta_Days(1,1,1,$_[0]-1); $day; }

Replies are listed 'Best First'.
Re: HTML::CalendarMonth date sequence not printing
by pg (Canon) on Oct 21, 2005 at 05:10 UTC

    For certain sequences, your while($year<$year2 || $month1 <= $month2) can form a dead loop.

    Inside your loop, the value for $month2 is unchanged, and $month1 goes between 1 and 12. If $month2 equals to 12, $month1 <= $month2 is always true. As true || anything = true, whether $year < $year2 is true or false doesn't make any difference, and the while becomes a dead loop.

      Thanks for the explanation - it's really useful. Much appreciated.
Re: HTML::CalendarMonth date sequence not printing
by GrandFather (Saint) on Oct 21, 2005 at 03:30 UTC

    Change your while loop to while($year<$year2 || ($year==$year2 && $month1<=$month2)).


    Perl is Huffman encoded by design.
      Thank you very much for your time, and for the solution to the problem. Much appreciated.

      Do you happen to know if it is possible to introduce style into the tables to get away from the old table grid look? I've had a look at the docs and the html intro page (excellent though it is)and can't see any way.

A reply falls below the community's threshold of quality. You may see it by logging in.