in reply to Populating an Array of hash's

The file names are the keys in your code, but you're printing the values.

But HTML::Template doesn't want a hash, it wants an array of records, where each record is a hash. In this case, the record only has one field, the file name.

<TMPL_LOOP NAME=FILES> Name: <TMPL_VAR NAME=NAME><br> </TMPL_LOOP>
$template->param( FILES => [ map +{ NAME => $_ }, @files ], );

If you also want a number, just add a field to the hash.

<TMPL_LOOP NAME=FILES> <TMPL_VAR NAME=NUM>. <TMPL_VAR NAME=NAME><br> </TMPL_LOOP>
$template->param( FILES => [ map +{ NUM => $_+1, NAME => $files[$_], }, 0..$#files ], );

Replies are listed 'Best First'.
Re^2: Populating an Array of hash's
by tinita (Parson) on Oct 02, 2008 at 10:14 UTC
    But HTML::Template doesn't want a hash, it wants an array of records, where each record is a hash.
    Just a note: If you use HTML::Template::Compiled you don't need that. It can iterate over an array of plain values, or even arrays of arrays. In this case:
    <%loop files %> <%= _ %> <%/loop files %>
Re^2: Populating an Array of hash's
by ianand0204 (Initiate) on Oct 01, 2008 at 15:47 UTC
    Thanks, that actually did the trick, but I have a question. What does the line below do? It looks like its taking the @files array and mapping them into a hash, which is getting mapped to the array FILES which HTML::Template then parses??
    $template->param( FILES => [ map +{ NAME => $_ }, @files ], );
    What is this line doing?
    map +{ NAME -> $_}

      map +{ NAME => $_ }, LIST
      is another way of writing
      map { { NAME => $_ } } LIST

      The expression { NAME => $_ }

      1. creates a hash,
      2. assigns NAME => $_ to it (creating a key named NAME whose associated value is $_), and
      3. returns a reference to the hash.

      The expression map { { NAME => $_ } } @files

      1. For every element of @files,
        1. sets $_ to the element
        2. executes the sub-expression.
      2. A list is formed from the result of the sub-expressions and returned.

      In other words,

      $template->param( FILES => [ map +{ NAME => $_ }, @files ], );

      could have been written

      my @files_param; for (@files) { push @files_param, { NAME => $_ }; } $template->param(\@files_param);