On Fri, 3 Sep 2004, Jamie Echlin wrote:

> I have a multi-level hash that I want to put in a table:
>
> [%
>       h.A = "AA";
>       h.X.Y = "XY";
> %]
>
> [% FOREACH col = ['A' 'X.Y'] %]
>       <TD>
>               [% h.$col %]
>       </TD>
> [% END; %]
>
> The X.Y key does not work - is there some special syntax I need to say
> treat this has a hash key to a hash key rather than a string or something?

Jamie,

This is something you'd have a hard time doing even in Perl.
Consider:

  my $hash = {};

  $hash->{ A } = 'AA';
  $hash->{ X }{ Y } = 'XY';

  foreach my $col ( ( 'A', 'X }{ Y' ) ) {
        print " <td>\n";
        print "         $hash->{ $col }\n";
        print " </td>\n";
  }

It seems like you'll either need a different separator (which
breaks the multi-level hash):

  [% h.A = 'AA' %]
  [% h.${ 'X.Y' } = 'XY' %]

  [% FOREACH col = ['A' 'X.Y'] %]
        <td>
                [% h.$col %]
        </td>
  [% END %]

or something that understands that the dot in the col value is
actually syntax (namely, eval)--something like:

  [% FOREACH col = ['A' 'X.Y'] %]
        <td>
                [% "[% h.$col %" | replace( '$', ']' ) | eval %]
        </td>
  [% END %]

There may be other, more clever solutions, but these are the ones
that came to mind for me.

Just out of curiosity, why do you need to show portions of a
multi-level hash in a flattened representation?

Hope that helps,

Dave

/L\_/E\_/A\_/R\_/N\_/T\_/E\_/A\_/C\_/H\_/L\_/E\_/A\_/R\_/N\
Dave Cash                              Power to the People!
Frolicking in Fields of Garlic               Right On-Line!
[EMAIL PROTECTED]                                  Dig it all.

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

Reply via email to