Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks,

Can anyone tell me how to change a hash in order for it to be fed to a <TMPL_LOOP> within HTML::Template? Here is what I am doing:

$img_number = $1; $img_user = $2; $hash{$img_number} = $img_user;
This happens several times, and then I have a large hash. Do I need to put this into an array and assign it names? I tried the code below:
my @loop_data = (); push(@loop_data, \%hash); my $template = HTML::Template->new(filename => 'fun.tmpl'); my $CGI = CGI->new(); $template->param(template_loop => \@loop_data); print $CGI->header( ); print $CGI->title("Here is your output."); print $template->output; exit;
and it didn't work. It gave the following error:
HTML::Template->output() : fatal error in loop output : HTML::Template + : Attempt to set nonexistent parameter 'URL229' - this parameter nam +e doesn't match any declarations in the template file : (die_on_bad_p +arams => 1) at /usr/share/perl5/HTML/Template.pm line 2789 at /var/www/QQ/funtimes.pl line 171

I am sure that an array is involved, but I dont know how to go from a has to an array. Is this what is needed?

Thank you.

Replies are listed 'Best First'.
Re: A hash to an array for HTML::Template
by jasonk (Parson) on Apr 20, 2005 at 22:29 UTC

    I don't know much about HTML::Template, but what you are passing it is a one-element array that contains a hash reference. I suspect you either want an array of the keys and values (my @loop_data = %hash), or an array of hash refs, in which case you probably just want to build it directly instead of building a hash first.

    # Instead of # $hash{$img_number} = $img_user; # # Try push(@loop_data, {$img_number => $img_user}); # or push(@loop_data, { img_number => $img_number, img_user => $img_user, }); # depending on what your template is expecting to see

    This way you end up with an array of hashes, each hash containing the data for one image.


    We're not surrounded, we're in a target-rich environment!

      Good show jasonk, and it does have to be an array of hashes to feed the <tmpl_loop>.


      —Brad
      "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
Re: A hash to an array for HTML::Template
by Cody Pendant (Prior) on Apr 21, 2005 at 00:38 UTC
    If you've only got one piece of information to put into the page, why do you need a loop anyway? And, can you please show us your template or the relevant portion thereof? That error message isn't saying you've fed it the wrong thing, it's saying you're trying to set parameters but there's no parameter which has the same name as your hash key.

    The basic structure of your loop and array should be something like this -- if you've got a page like this:

    FIRSTNAME LASTNAME --------------------- Fred Flintstone Barney Rubble

    Then you need an array like this:

    @loop_data = ( { firstname => 'Fred', lastname => 'Flintstone' }, { firstname => 'Barney', lastname => 'Rubble' } );

    and a template that looks like this:

    FIRSTNAME LASTNAME --------------------- <tmpl_loop name="foo"> <tmpl_var name="firstname"> <tmpl_var name="lastname"> </tmpl_loop>


    ($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
    =~y~b-v~a-z~s; print