On Wednesday, June 25, 2003, at 06:18 PM, Bernard Golden wrote:

I wish to create a table with the year on the left, and the counts for each of the subsequent years strung out to the right of that year.� I can't quite figure out how to handle anonymous hashes in templates, so I was hoping someone could give me a pointer, or a code fragment in which they did something similar.� Alternatively, if it would be better to create a different form of data structure, I could do that, but I'm sure there must be a way to accomplish what I want to do.


I think what you need to do is first loop through list of hashes of hashes, and collect all the
years where future events occur, then loop through the first level hash again and look up if it
has that event.


This solution falls down if there are gaps in years in either the rows or columns.
If it is necessary to accommodate gaps, you would need to loop around to find
the min and max of the event years, and then iterate from min to max
when writing out the values for the column headers and each set of row
values.


[%
   # first, collect all the columns of the table
   FOREACH  datum = data ;
       targetyear = datum.keys.0;
       FOREACH eventyear = datum.$targetyear.keys;
            hasaneventyear.$eventyear = 1;
       END;
   END;
   alleventyears = hasaneventyear.keys.sort;
%]

<TABLE border="2">
    <TH>
[% FOREACH eventyear = alleventyears  %]
        <TD>[% eventyear %]</TD>
[% END %]
    </TH>

[% # now for each year as a row, look up if it has any events and print them out in the column
FOREACH datum = data %]
[% targetyear=datum.keys.0 %]
<TR>
<TD>[% targetyear %]</TD>
[% FOREACH eventyear = alleventyears %]
<TD> [% datum.$targetyear.$eventyear %] </TD>
[% END %]
</TR>
[% END %]



_______________________________________________ templates mailing list [EMAIL PROTECTED] http://lists.template-toolkit.org/mailman/listinfo/templates

Reply via email to