Re: tt hash values not printing....

Rajeev Prasad <rp.neuli-/[email protected]> Tue, 30 Oct 2012 17:19:38 -0700 (PDT)
Newsgroups gmane.comp.lang.perl.modules.template-toolkit
Message-ID <[email protected]>
hi Tom,

I just solved (found issue) with this.

I changed the way i was building the hash value: and then it worked fine.

$my_hash->{$index} = [$val1, $val2];

Also i looked and found nsort which resolved the other prob too.

but i have a new problem now. I am trying to break a long string (val2) on the first html break character (<br>) I am using this logic but it is not working. it seems my regex is wrong. i have tried many things. but it does not owrk. I get nothing for matches.1 can you suggest ?

    [% IF (matches = myhash.$index.1.match('(.*<br>)(.*$)')) %]
    <td><tt>[%- matches.0 -%]</tt><tt style="color:green;">[%- matches.1 -%]</tt></td>
    [% ELSE %]
    <td><tt>[%- myhash.$index.1 -%]</tt></td>
    [% END %]





----- Original Message -----
From: Tom Molesworth <tom-UE50auxAeIt4LjgWlUEQQwC/[email protected]>
To: Rajeev Prasad <rp.neuli-/[email protected]>
Cc: perl_TT <[email protected]>
Sent: Tuesday, October 30, 2012 6:17 PM
Subject: Re: [Templates] tt hash values not printing....

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