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

Running into a problem while recoding all my scripts to use HTML::Tempalte (thus removing all HTML from the coding itself)...

The following code takes %bnb, and sorts the keys by the values assigned to them, and then assigns any key/value pair that doesn't have a 0 value to a variable that I want to send to HTML template as a LOOP element:

my %bnb = {Seals => 0, Fighters => 0, Panzers => 0, 'Heavy Lasers' => +0, Marines => 0, Bombers => 0}; my $bestNw; foreach my $key (sort { $bnb{$a} <=> $bnb{$b} } keys %bnb) { if ($bnb{$key} > 0) { my $value = addComma($bnb{$key}); push @$bestNw, { unit => $key, value => $value }; } }

I'm running into a problem though... when I try to send $bestNW with:

$template->param(bestNw => $bestNw);

where bestNw is called using: <TMPL_LOOP NAME=bestNw> in the template file, I'm getting the following error:

"HTML::Template::param() : attempt to set parameter 'bestnw' with a scalar - parameter is not a TMPL_VAR! at /apache/cgi-bin/test.cgi line 241"

Any suggestions would be greatly appreciated.


Steny
  • Comment on Problem sorting hash & assigning to new hash to send to HTML::Template
  • Download Code

Replies are listed 'Best First'.
Re: Problem sorting hash & assigning to new hash to send to HTML::Template
by borisz (Canon) on Jun 06, 2004 at 21:14 UTC
    I think your hash %bnb has _no_ keys. Try
    $template->param( bestNw => $bestNw || [] );
    Boris
Re: Problem sorting hash & assigning to new hash to send to HTML::Template
by neniro (Priest) on Jun 06, 2004 at 21:18 UTC
    I suggest you to use Data::Dumper and CGI::Carp. Put die Dumper($bestNw); after your foreach-loop to see whats in your variable $bestNw at this time.
Re: Problem sorting hash & assigning to new hash to send to HTML::Template
by bradcathey (Prior) on Jun 06, 2004 at 20:37 UTC
    Chances are that error is telling you 1) you need to assign the loop a AoH ref, or 2) you are assigning it an undefined value. At least this has been my experience in the last couple of weeks. It might be helpful to see your HTML.

    —Brad
    "A little yeast leavens the whole dough."
Re: Problem sorting hash & assigning to new hash to send to HTML::Template
by eric256 (Parson) on Jun 07, 2004 at 06:09 UTC

    Your sample data is setting all the values to 0 and then you are only pushing onto the arrayref when the value is larger than zero. Try setting some of those to something other than 0 and see what you get.


    ___________
    Eric Hodges