[Templates] Multiple Dimension Arrays
Mark Mills
extremely@hostile.org
Wed, 20 Dec 2006 11:01:48 -0500
This is a multi-part message in MIME format.
--------------090402040801000707040900
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Bullock Stuart wrote:
>
> To Whom It May Concern:
>
>
>
> 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.
>
>
>
> 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.
>
>
>
> Regards,
>
>
>
> Stuart Bullock
>
Here's my solution, slightly simplified.
The data:
my %vars = (
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 template:
[% FOREACH line = matrix -%]
[%- line.join(',') %]
[% END -%]
The output:
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.9,0
I suppose, if you wanted to format individual values, you could do this:
[% FOREACH line = matrix -%]
[% FOREACH item = line -%]
[%- item %][% UNLESS loop.last; ","; ELSE; "\n"; END %][% END -%]
[% END -%]
--mark
--------------090402040801000707040900
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Bullock Stuart wrote:
<blockquote
cite="midDB7BEF2CBAD51844A7FD7035E301548116396F65@gbr2-ldn05.uk.mizuho-sc.com"
type="cite">
<meta http-equiv="Content-Type" content="text/html; ">
<meta name="Generator" content="Microsoft Word 10 (filtered)">
<style>
<!--
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman";}
a:link, span.MsoHyperlink
{color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{color:purple;
text-decoration:underline;}
p.MsoPlainText, li.MsoPlainText, div.MsoPlainText
{margin:0cm;
margin-bottom:.0001pt;
font-size:10.0pt;
font-family:"Courier New";}
span.EmailStyle17
{font-family:Arial;
color:windowtext;}
@page Section1
{size:612.0pt 792.0pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;}
div.Section1
{page:Section1;}
-->
</style>
<div class="Section1">
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB">To Whom It
May Concern:</span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB"> </span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB">I have a
problem in attempting to access a matrix
style array.</span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB"> </span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB">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.</span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB"> </span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB">Data
Structure</span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB">===========</span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB"> </span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB">dataFile
=
(</span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB">
name =>
“Curve Matrix”</span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB">
,matrix =>
[</span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB">
[
0 , 365 , 1095 , 1825 , 3650 , 2555 ]</span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB">
,[2.3
, 0.142 , 0 , 0 ,
0 , 0 ]</span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB">
,[2.4
, 0 , 1.72 ,
0 , 0 , 1.71 ]</span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB">
,[2.5
, 0 ,
0 , 1.75 , 1.90 ,
0 ]</span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB">
]</span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB"> </span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB">
);</span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB"> </span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB">Output</span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB">======</span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB">What I
should like on the output is:</span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB"> </span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB">0,365,1095,1825,3650,255</span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB">2.3,0.142,0,0,0,0</span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB">2.4,0,1.72,0,0,1.71</span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB">2.5,0,0,1.75,1.90,0</span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB"> </span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB">Naive
Attempted Solution</span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB">==================</span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB"> [%-
USE matrix =
iterator(dataFile.matrix) %]</span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB"> [%
FOREACH
line = matrix %]</span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB">
[%- FOREACH column = line %]</span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB">
[%- %][% column %] [% IF NOT column.last %],[% END %]</span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB">
[%- END %]</span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB"> [%
END %]</span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB"> </span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB">As stated –
any help or pointers with the
syntax welcome.</span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB"> </span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB">Regards,</span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB"> </span></font></p>
<p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size: 10pt; font-family: Arial;" lang="EN-GB">Stuart
Bullock</span></font></p>
</div>
<meta content="TX_HTML32 11.0.211.501" name="GENERATOR">
<title></title>
</blockquote>
Here's my solution, slightly simplified. <br>
<br>
The data: <br>
my %vars = (<br>
name => "Curve Matrix."<br>
,matrix => [<br>
[ 0 , 365 , 1095 , 1825 , 3650 , 2555 ]<br>
,[2.3 , 0.142 , 0 , 0 , 0 , 0 ]<br>
,[2.4 , 0 , 1.72 , 0 , 0 , 1.71 ]<br>
,[2.5 , 0 , 0 , 1.75 , 1.90 , 0 ]<br>
]<br>
);<br>
<br>
The template:<br>
[% FOREACH line = matrix -%]<br>
[%- line.join(',') %]<br>
[% END -%]<br>
<br>
The output:<br>
0,365,1095,1825,3650,2555<br>
2.3,0.142,0,0,0,0<br>
2.4,0,1.72,0,0,1.71<br>
2.5,0,0,1.75,1.9,0<br>
<br>
I suppose, if you wanted to format individual values, you could do this:<br>
[% FOREACH line = matrix -%]<br>
[% FOREACH item = line -%]<br>
[%- item %][% UNLESS loop.last; ","; ELSE; "\n"; END %][% END -%]<br>
[% END -%]<br>
<br>
<br>
--mark<br>
</body>
</html>
--------------090402040801000707040900--