in reply to Re: Got those HTML::Template and subroutine blues
in thread Got those HTML::Template and subroutine blues

That works like a charm.   chipmunk - you are wise beyond your size.   {grin}

I oversimplified my script for the post, though.   Was using Tie::IxHash for insertion-order retrieval of hash elements.

use Tie::IxHash; tie my %urlhashA, "Tie::IxHash"; %urlhashA = ( 'blah' => 'URL', 'bleh' => 'notherURL', 'bloo' => 'ananotherURL', );
I didn't spot anything in perldoc IxHash.pm about using it with hash o' hashes.  
tie my %tmpl_hash, "Tie::IxHash"; did nothing noticeable.  
Attempts to tie urlloop failed as well.

Is this do-able?

Update: chipmunk's post below puts a glide in my stride.   8^)   chipmunk++.
    cheers,
    Don
    striving for Perl Adept
    (it's pronounced "why-bick")

  • Comment on Re: (2) Got those HTML::Template and subroutine blues (chipmunk++)
  • Download Code

Replies are listed 'Best First'.
Re: Re: (2) Got those HTML::Template and subroutine blues (tie hash o' hashes?)
by chipmunk (Parson) on Dec 23, 2000 at 00:48 UTC
    Ah, that makes sense, because you want the HTML output to be in the right order. The tricky thing is that the outer hash and each of the inner hashes must be tied to Tie::IxHash... I'm trying to imagine the code for that, and it's not coming out very pretty. :)

    So, I'll propose an array of arrays solution instead.

    my @tmpl_array = ( urlloopA => [ 'Home' => '/', 'Icons' => '/icons/', 'Sitedocs' => '/doc/', ], urlloopB => [ 'Cisco' => 'http://www.cisco.com/', 'CPAN' => 'http://search.cpan.org/', 'Google' => 'http://www.google.com/', 'Perl Monks' => 'http://www.perlmonks.org/', ], ); for (my $i = 0; $i < $#tmpl_array; $i+=2) { my($loop, $aref) = @tmpl_array[$i, $i+1]; my @vars; for (my $j = 0; $j < $#{$aref}; $j+=2) { my($name, $url) = @{$aref}[$j, $j+1]; push @vars, { name => $name, url => $url }; } $template->param($loop, [ @vars ]); }
    We're sort of simulating the ordered hashes, by having paired elements in each of the arrays. This works because we don't actually need any hash key lookups!

    Update: This code would be used with the same updated template as the hash of hashes suggestion in my earlier reply.

      Personally I avoid using the C-style for, since I find it harder to see exactly what what it is doing at a glance. I also avoid loop indexes as they aren't very perlish. This is another way to do it, but it destroys the arrays, which may or may not be acceptable.
      while ( @tmpl_array ) { my ($loop, $aref) = splice @tmpl_array,0,2; my @vars; while (@$aref) { my ($name, $url) = splice @$aref,0,2; push @vars, { name => $name, url => $url }; } $tmpl->param($loop, \@vars; }