in reply to Re: Re: Generating a container table in HTML::CalendarMonth
in thread Generating a container table in HTML::CalendarMonth

I think I have solved your problem jonnyfolk.

#!/usr/bin/perl use strict; use warnings; use CGI qw( :standard ); use HTML::CalendarMonth; use HTML::AsSubs; use HTML::Element; use Date::Calc qw( :all ); # @dates has already been sorted my @dates = ( 730000, 731158, 731159, 731160, 731161, 731178, 735000 ) +; my ( @cals, @days ); print start_html(-title => 'Calendars'); while ( defined $dates[0] ) { my $days = $dates[0]; my ( $year, $month, $day ) = Add_Delta_Days( 1, 1, 1, $days - 1 ); my $new_c = new HTML::CalendarMonth ( year => $year, month => $month + ); @dates = CreateCal( $year, $month, @dates ); push( @cals, $new_c ); } # Embolden headers foreach my $calnum ( 0 .. $#cals ) { my $cal = $cals[$calnum]; my @color = @{ $days[$calnum] }; # the days to be colored $cal->item( $cal->month, $cal->year )->wrap_content( font({ size => + '2' }) ); $cal->item( $cal->dayheaders )->wrap_content( font({ size => '1' }) + ); $cal->item( @color )->wrap_content( font({ size => 4, color => 'red +' }) ); } # Generate container table my $t = new HTML::ElementTable ( maxrow => 1, maxcol => 4 ); # Populate container table for my $calnum ( 0 .. $#cals ) { $t->cell( 0, $calnum )->push_content( $cals[$calnum] ); } print $t->as_HTML; print end_html(); exit(0); sub CreateCal { my ( $cyear, $cmonth, @dates ) = @_; my ( @day, @temp ); foreach my $days ( @dates ) { my ( $year, $month, $day ) = Add_Delta_Days( 1, 1, 1, $days - 1 ); if ( $year == $cyear && $month == $cmonth ) { push ( @day, $day ); } else { push ( @temp, $days ); } } push ( @days, [ @day ] ); # store the days of each calendar return @temp; }

  • Comment on Possible Solution for Generating a container table in HTML::CalendarMonth
  • Download Code

Replies are listed 'Best First'.
Re: Possible Solution for Generating a container table in HTML::CalendarMonth
by jonnyfolk (Vicar) on Apr 01, 2003 at 10:20 UTC
    Hello, Mr Muskrat, sorry about the delay in getting back to you - I've been waiting for some time to go through this properly.

    Firstly this is a definite solution to the problem - it is throwing the calendars, with their specific day arrays into a container table.
    Secondly, I don't understand it!!

    Well, there are one or two things that are new to me and perhaps if you have a little time you wouldn't mind explaining them to me?

    In the sub CreateCal: push ( @days, [ @day ] ); # store the days of each calendar
    you're storing an array into and array, I thought that would need a %hash. What is the significance of the [ ]?

    After #Embolden headers, the line: my @color = @{ $days[$calnum] }; # the days to be colored
    seems to be a hash of some sort but I'm afraid I don't quite get it. (I do get the fact that you are storing an array of dates and then extracting them, but I don't understand the mechanics).

    I'm very impressed at the simplicity of what you have achieved compared to the Heath Robinson approach I had embarked upon - this looks like the real thing.

    I have also been trying very hard to get the calendars to fill different rows:
    the line  $t->cell( 0, $calnum )->push_content( $cals[$calnum] ); pushes all the calendars along a single row.

    If we generate a new table:

    # Generate container table my $t = new HTML::ElementTable ( maxrow => 12, maxcol => 3 );
    then how can I get the calendars to fill across and then down to the next row?

    By using  $t->cell( $calnum, $calnum )->push_content( $cals[$calnum] );
    the calendars fill diagonally, and I had hoped that by using

    # Populate container table for my $calnum ( 0 .. $#cals ) { my $row = int(my $calnum/4); $t->cell( 0, $calnum )->push_content( $cals[$calnum] ); }
    that might do the trick, but I am left with a blank screen. In fact everything I have tried pretty much leaves me with a blank screen!!

    If you have any ideas on the latter and perhaps give an explanation of the former I would be very grateful

    Many thanks, though, for your help so far...

      Basicly, I'm creating an array of array references and then dereferencing them when it's time to use them. Fear not! Arrays of Arrays only sound scary. perlref, perlreftut, perldsc, perllol and References quick reference are all very informative!

      Here's what I think will be the finished product. We calculate the row and column based on the value of $calnum. Enjoy!

      #!/usr/bin/perl use strict; use warnings; use CGI qw( :standard ); use HTML::CalendarMonth; use HTML::AsSubs; use HTML::Element; use Date::Calc qw( :all ); # @dates has already been sorted my @dates = ( 730000, 731158, 731159, 731160, 731161, 731178, 731300, +731310, 731400, 731410, 731500, 731510, 735000 ); my ( @cals, @days ); print start_html(-title => 'Calendars'); while ( defined $dates[0] ) { my $days = $dates[0]; my ( $year, $month, $day ) = Add_Delta_Days( 1, 1, 1, $days - 1 ); my $new_c = new HTML::CalendarMonth ( year => $year, month => $month + ); @dates = CreateCal( $year, $month, @dates ); push( @cals, $new_c ); } # Embolden headers foreach my $calnum ( 0 .. $#cals ) { my $cal = $cals[$calnum]; my @color = @{ $days[$calnum] }; # the days to be colored $cal->item( $cal->month, $cal->year )->wrap_content( font({ size => + '2' }) ); $cal->item( $cal->dayheaders )->wrap_content( font({ size => '1' }) + ); $cal->item( @color )->wrap_content( font({ size => 4, color => 'red +' }) ); } # Generate container table my $t = new HTML::ElementTable ( maxrow => 12, maxcol => 3 ); my $row = 0; # Populate container table for my $calnum ( 0 .. $#cals ) { my $col = $calnum % 3; my $row = int( $calnum / 3 ); $t->cell( $row, $col )->push_content( $cals[$calnum] ); } print $t->as_HTML; print end_html(); exit(0); sub CreateCal { my ( $cyear, $cmonth, @dates ) = @_; my ( @day, @temp ); foreach my $days ( @dates ) { my ( $year, $month, $day ) = Add_Delta_Days( 1, 1, 1, $days - 1 ); if ( $year == $cyear && $month == $cmonth ) { push ( @day, $day ); } else { push ( @temp, $days ); } } push ( @days, [ @day ] ); # store the days of each calendar return @temp; }

        It is, indeed, as you say - "The Final Solution". I read in awe...

        I'm pleased at least that I was on the right lines with  my $row = int( $calnum / 3 );. Unfortunately I had meant to show you the lines:

        # Populate container table for my $calnum ( 0 .. $#cals ) { my $row = int($calnum/4); $t->cell( $row, $calnum )->push_content( $cals[$calnum] ); }
        but I was in too much of a hurry and pasted the wrong thing:(
        I see belatedly that this couldn't have worked because the column numbers are only ever 1 to 3 whereas $calnum is constantly increasing. I have looked in Programming Perl and discovered the modulus operand, but the explanation is rather too opaque for me to understand. Could you explain how it achieves its goal in this case?

        Thanks once again for this - I shall not only enjoy - I am thrilled to use it and only hope that one day I might aspire to such things myself.