Re: tt hash values not printing....

Tom Molesworth <tom-UE50auxAeIt4LjgWlUEQQwC/[email protected]> Tue, 30 Oct 2012 23:17:26 +0000
Newsgroups gmane.comp.lang.perl.modules.template-toolkit
Message-ID <[email protected]>
Hi there,

On 30/10/12 20:07, Rajeev Prasad wrote:
> starting with a hash ref....
>
> my $my_hash = {};
>
>
> building the hash in a loop...
>
>
>            push(@tmp_arr,$val1);
>            push(@tmp_arr,$val2);
>            $my_hash->{$index} = \@tmp_arr;
>
>
>
> the elements for any key can be access fine as   $my_hash->{$index}[0]
>
>
>
> I am passing a hash like this:
>
>      my $vars = {
>         myhash => $my_hash
>      };
>
>
>
> PROBLEM 1: trying to print like this, it prints the index correctly but does not print any values.... i am confused why?
>
>      [%- FOREACH index IN myhash.keys.sort -%]
>      <tt>index number=[%index%]  dt-time=[%myhash.$index.0%]  detail=[%myhash.$index.1%]<tt>
>      [% END -%]


Seems fine here:

$ perl -MTemplate -le'Template->new->process(\q{[% FOR index IN 
myhash.keys.sort %]Index [% index %], values [% myhash.$index.0 %] and 
[% myhash.$index.1 %][% END %]}, { myhash => { 0 => [qw(x y)] } })'
Index 0, values x and y

You tried checking what's in $vars (using Data::Dumper or equivalent) to 
confirm the values are getting to the template? Also, what's the scope 
of @tmp_arr? Are you sure you're using a different array for each hash 
slot? You can test that easily by replacing \@tmp_arr with [ @tmp_arr ] 
(and again I'd suggest renaming the variable).

>
> PROBLEM 2: the sort is not sorting numerically, so i get 1,10,11,12.... 2,20,21.....
>
See perldoc Template::Manual::VMethods - you probably want the nsort 
method instead.

cheers,

Tom