"Bullock Stuart" <[EMAIL PROTECTED]> writes:

> I have a problem in attempting to access a matrix style array.
>
> If anyone can help I should be very grateful.  If I have posted to the
> wrong email address, my apologies and please advise me of the correct
> email address to TT2 problems to.

That's ok - this is the correct mailing list.

> Data Structure
>
> ===========
>
> dataFile     = (
>                      name               => "Curve Matrix"
>                     ,matrix              => [
> [ 0   ,  365    , 1095 , 1825 , 3650 , 2555  ]
> ,[2.3 ,  0.142 ,  0      , 0      , 0      , 0       ]
> ,[2.4 ,  0       ,  1.72  , 0      , 0      , 1.71   ]
> ,[2.5 ,  0       ,  0      , 1.75  , 1.90  , 0       ]
>                                                       ]
>                            );
>
> Output
> ======
>
> What I should like on the output is:
>
> 0,365,1095,1825,3650,255
> 2.3,0.142,0,0,0,0
> 2.4,0,1.72,0,0,1.71
> 2.5,0,0,1.75,1.90,0
>
> Naive Attempted Solution
> ==================
>
>        [%- USE matrix = iterator(dataFile.matrix) %]
>         [% FOREACH line = matrix %]
>             [%- FOREACH column = line %]
>                 [%- %][% column %] [% IF NOT column.last %],[% END %]
>             [%- END %]
>         [% END %]
>
> As stated - any help or pointers with the syntax welcome.

I'd say your approach isn't naive *enough*.  I don't know exactly how
much of your example you've simplified before posting, but for what
you wrote the solution is *much* easier than that.

To check whether I've understood your problem, here is a complete
program which should do what you want, with two different versions of
the template in the __DATA__ section:
----------------------------------------------------------------------
#!/usr/bin/perl -w
use strict;

use Template;

my $tt = Template->new();

my $dataFile = {name   => "Curve Matrix",
               ,matrix =>
                [[0,365,1095,1825,3650,2555],
                 [2.3,0.142,0,0,0,0],
                 [2.4,0,1.72,0,0,1.71],
                 [2.5,0,0,1.75,1.90,0],
                ]
                };

$tt->process(\*DATA,{dataFile => $dataFile}) or die $tt->error();

__DATA__
[%- FOREACH line = dataFile.matrix %]
    [%- FOREACH column = line %]
        [%- column %][% IF NOT loop.last %],[% END %]
    [%- END %]
[%  END %]

[%- FOREACH line = dataFile.matrix %]
    [%- GET line.join(',') %]
[% END %]
----------------------------------------------------------------------

Explanation:

 * If you create an iterator, you must apply the 'last' method on that
   iterator, and not on the index variable.
 * You do not need to create an explicit iterator unless you want to
   apply the iterator methods on the *outer* loop.  There's an
   iterator named 'loop' created automatically for each loop.
 * And finally, there's the 'join' method which operates on a list
   (one line in your case) and does exactly the same as your inner
   loop.
-- 
Hope this helps,
haj

_______________________________________________
templates mailing list
[email protected]
http://lists.template-toolkit.org/mailman/listinfo/templates

Reply via email to