Re: [Templates] multilevel hash to table

[prev] [thread] [next] [Date index for 2004/09/03]

From: Dave Cash
Subject: Re: [Templates] multilevel hash to table
Date: 13:38 on 03 Sep 2004
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!
dave@xxxxx.xxx                                  Dig it all.

_______________________________________________
templates mailing list
templates@xxxxxxxxxxxxxxxx.xxx
http://lists.template-toolkit.org/mailman/listinfo/templates

[Templates] Simple Error Question
Denis Banovic 09:01 on 03 Sep 2004

Re: [Templates] Simple Error Question
Dave Cross 09:05 on 03 Sep 2004

[Templates] multilevel hash to table
Jamie Echlin 10:26 on 03 Sep 2004

Re: [Templates] multilevel hash to table
Dave Cash 13:38 on 03 Sep 2004

Generated at 08:55 on 15 Mar 2005 by mariachi v0.52