Harald,

Thank you for your reply.  Your understanding of the problem is correct
and the solution is excellent (better than mine).  My problem before
posting was (embarrassing as it is to admit on a public forum) that I
had not properly constructed my data before calling the template.

The perl I had could be listed as follows:

== Perl snippet ==
$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       ]
);

## The next line was my problem - D'oh!
$dataFile->{matrix}             = @matrix; 


And the perl template was correctly producing the result "4" when I ran
my query.  The correct perl, of course, is:

$dataFile->{matrix}             = [EMAIL PROTECTED]; #gives reference instead of
count

Because I have only started using perl templates from last week (and
quite happily , I add!!) I thought it was a problem in how I using the
template rather than in forming the data.  Mia culpa.

Once again - many thanks,

Stuart Bullock

-----Original Message-----
From: Harald Joerg [mailto:[EMAIL PROTECTED] 
Sent: 20 December 2006 16:21
To: Bullock Stuart
Cc: [email protected]; Lyons Neil
Subject: Re: [Templates] Multiple Dimension Arrays

"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

This message and any files transmitted with it are confidential and intended 
solely for the use of the individual or entity to whom they are addressed. If 
you have received this message in error please delete it and any files 
transmitted with it, after notifying [EMAIL PROTECTED] 
Any opinions expressed in this message may be those of the author and not 
necessarily those of the company. The company accepts no responsibility for the 
accuracy or completeness of any information contained herein. This message is 
not intended to create legal relations between the company and the recipient. 
Recipients should please note that messages sent via the Internet may be 
intercepted and that caution should therefore be exercised before dispatching 
to the company any confidential or sensitive information. 
Mizuho International plc Bracken House, One Friday Street, London EC4M 9JA. 
TEL. 020 72361090. Wholly owned subsidiary of Mizuho Securities Co., Ltd. 
Member of Mizuho Financial Group. Authorised and regulated by the Financial 
Services Authority. Member of the London Stock Exchange. 

Registered in England No. 1203696. Registered office as above.


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

Reply via email to