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

I'm new to this so please bear with me. I have a function that takes an array reference as an argument (HTML::Template). I need to pass many arrays to this func in a loop. My first guess was to simply make a hash of arrays and than pass the references of elements to the func, but it complains that i'm passing a scalar (even if the element starts with '@' and I' know' it's an array.
%param{'key'} = @array; $template->param('HTMLloop'=>\@param{'key'};
i'm using several nested <TMPL_LOOP>'s, so each hash element form above (array) will be all data for one <TMPL_LOOP> Any Ideas on this? Thanks.

Replies are listed 'Best First'.
Re: Using arrays as elements in hash
by Fletch (Bishop) on Oct 30, 2007 at 18:41 UTC

    Several things:

    • %param{'key'} is just wrong; you mean $param{ 'key' } (I'm going to presume this was a typo or cut-and-pasteo)
    • Likewise @param{'key'} while actually syntactically valid is a single element hash slice; again you probably want $param{ 'key' } here as well
    • More than likely what you're calling expects an array reference, so you probably want $param{ 'key' } = \@array; (or possibly $param{'key'} = [ @array ] if you're worried that what you're calling might diddle the contents of the original @array)

    See perlreftut and perlref for more details (and possibly swing through perldata for a refresher).

      I might of missed something, but, to pass more references I need a different @array for every pass of the loop, because, if I don't, all the elements of the hash will point to one @array. And when I pass this to the func... it's just not right. (all <TMPL_LOOP>'s will use the same reference array (@array), and that's not correct).

      What I wanted to do is make a hash of arrays and then for each loop pass, pass a reference to an element. That way all <TMPL_LOOP>'s get its individual and corret data. Is this possible .(the're might be other ways but I don't know about them...

        You right. You missed something. Here small part from HTML::Template docs:

        TMPL_LOOP

        <TMPL_LOOP NAME="LOOP_NAME"> ... </TMPL_LOOP>

        The <TMPL_LOOP> tag is a bit more complicated than <TMPL_VAR>. The <TMPL_LOOP> tag allows you to delimit a section of text and give it a name. Inside this named loop you place <TMPL_VAR>s. Now you pass to param() a list (an array ref) of parameter assignments (hash refs) for this loop. The loop iterates over the list and produces output from the text block for each pass. Unset parameters are skipped. Here's an example:

         In the template:
           <TMPL_LOOP NAME=EMPLOYEE_INFO>
              Name: <TMPL_VAR NAME=NAME> 
        Job: <TMPL_VAR NAME=JOB>

        </TMPL_LOOP> In the script:

        $template->param(EMPLOYEE_INFO => [ { name => 'Sam', job => 'progra +mmer' }, { name => 'Steve', job => 'soda + jerk' }, ] ); print $template->output();

        So you need array of hashes. If you show more code we can help you.

        As Fletch suggest you need

        $template->param( 'HTMLloop' => \@array ); or $template->param( 'HTMLloop' => [ @array ] );

        Where @array consists of hash refs.