in reply to Re^2: Using arrays as elements in hash
in thread Using arrays as elements in hash

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.

Replies are listed 'Best First'.
Re^4: Using arrays as elements in hash
by XMas (Initiate) on Oct 31, 2007 at 09:13 UTC
    Ok, here's what I got so far.

    foreach my $Temp (@IsNew){ my %param_2 = ( 'Flag'=>$Temp, #<-- TEST ); foreach my $product (@Products) { my %param = ( 'Code'=>$product, } push(@loop_data_1, \%param); } $loop_data_3{$counter} = @loop_data_1; $param_2{'ProductList'} = \$loop_data_3{$counter}; push(@loop_data_2, \%param_2); @loop_data_1 = (); } $tmpl->param('NumOfProductLists'=>\@loop_data_2);

    @loop_data_1 contains the data for the inner loop
    @loop_data_2 countain the data for the superior loop (inside it should be arbitray number of different @loop_data_1 arrays). Basically i want to dinamicly add drop down boxes with different \<selected\> elements. That's why I think I need a different @loop_data_1 for each @loop_data_2.

    This is where I thought of using or an array of arrays or a hash of arrays...

      I'm think this is wrong (it's looks very strange for me):

      $loop_data_3{$counter} = @loop_data_1; $param_2{'ProductList'} = \$loop_data_3{$counter};

      Maybe you want:

      $loop_data_3{$counter} = \@loop_data_1; $param_2{'ProductList'} = $loop_data_3{$counter};
        Nope, that's the first thing I tryed. What that does is give every hash element a reference to @loop_data_1, whitch means that all the <TMPL_LOOP>'s will use a reference to the same array

        $loop_data_3{1} = \@loop_data_1;
        $loop_data_3{2} = \@loop_data_1; (same data, not what I need)