in reply to Perl Template + html: hash keys and values keyword not working

[% FOREACH item IN report.rows.values %] <tr> <td>[% item.id %]</td> <td>[% item.delta %] %</td> </tr> [% END %]

The values of the report.rows hash are not themselves hashes, but arrays. Therefore item.id makes no sense. item.0.id is more likely to be what you are after.

If you want to iterate over all items in the arrays you'll need a nested loop or some other means of iterating inside them. eg.

#!/usr/bin/env perl use strict; use warnings; use Template; my $r = { 'rows' => { 'category1' => [ {'id' => 'AAA', 'delta' => '0.01%'}, {'id' => 'BBB', 'delta' => '0.03%'} ], 'category2' => [ {'id' => 'CCC', 'delta' => '0.02%'}, {'id' => 'DDD', 'delta' => '0.04%'} ], }, }; my $text = q# [% FOREACH item IN report.rows.values %] [% FOREACH one IN item %] <tr> <td>[% one.id %]</td> <td>[% one.delta %] %</td> </tr> [% END %] [% END %] #; my $tt = Template->new; $tt->process (\$text, {report => $r});

Replies are listed 'Best First'.
Re^2: Perl Template + html: hash keys and values keyword not working
by feiiiiiiiiiii (Acolyte) on May 08, 2016 at 02:22 UTC

    Yes it is! Can't believe I'm making such a stupid bug :( It's working now. Thank you so much!

      Can't believe I'm making such a stupid bug

      We only make progress at anything worthwhile by making mistakes...

      That "doh!" we feel when we realise our mistake is actually out brain rewiring itself following an important moment of learning.

        That "doh!" we feel when we realise our mistake is actually out brain rewiring itself following an important moment of learning.

        ... unless you are Homer Simpson ;-)

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)