[Templates] tpage and header line in a .txt file

Teun-Arno A.Teeling3@chello.nl
Sat, 18 Nov 2006 15:42:20 +0100


This is a multi-part message in MIME format.

------=_NextPart_000_0002_01C70B28.22907B80
Content-Type: text/plain;
	charset="us-ascii"
Content-Transfer-Encoding: 7bit

Hello
 
Example_File : machines.txt
======================
name,     os,          ip,            Connect_with,   Customer
apollo,   RedHat 7.3,  10.100.5.100,   Telnet,        Frieslandbank
hera,     RedHat 7.2,  10.100.33.227,  SSH,           PON
juno,     Solaris 8,   10.100.6.41  ,  SSH ,          SNV
artemis,  RedHat 7.3,  10.100.6.42  ,  Telnet,        BLA
 
And wanted from the  "machines.txt" a .html file from it with headers in the
order that they are in the machines.txt file. 
 
Wrote the next code to do so because I could not get at the headers ( name ,
os , ip ... ) in the order they are in the machines.txt file. (
machines.keys gave nothing back , machines.0.key gives them back in random
order , yes it's a hash. ) 
 
 
Template file : machines.tt
=====================
[% USE machines = datafile('machines.txt',  delim = ',') %]
[% PERL %]
my @lables=();
my $headers;
   open ( TXT , "< machines.txt " ) or die "no such file"  ;
   @lables=split(/\s*,\s*/,readline ( TXT ));  # assume headers are on the
first line
  close TXT; 
  chomp $lables[$#lables];
  # export to the namespace of Template ?, it , seems to do so
  $stash->set( headers => \@lables );    
[% END %]

<HTML><BODY>
<Table border=1 cellpadding=0 cellspacing=0
STYLE="font-family:Arial;font-size:9px">
<TR>
[% FOREACH lable IN headers -%]
<TH bgcolor="yellow">[% lable %]</TH>
[% END -%]
</TR>
[% FOREACH machine IN machines -%]
<TR>
[%- FOREACH lable IN headers %]
[% IF machine.$lable == "" %]
 <TD>&nbsp</TD>
[%- ELSE -%]
<TD>[% machine.$lable %]</TD>
[%- END %]
[%- END %]
 
 
Now I must run it : tpage --eval_perl machines.tt
Is there a more elegant way to get at the headers in order they are in the
machines.txt. 
 
Thanks in advance : Arno Teunisse
 

------=_NextPart_000_0002_01C70B28.22907B80
Content-Type: text/html;
	charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Dus-ascii">
<META content=3D"MSHTML 6.00.5700.6" name=3DGENERATOR></HEAD>
<BODY>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D203123114-18112006>Hello</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D203123114-18112006></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2><SPAN =
class=3D203123114-18112006>Example_File :=20
machines.txt</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D203123114-18112006>=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D</SPAN></FONT></DIV>
<DIV><FONT face=3DCourier size=3D2><SPAN=20
class=3D203123114-18112006>name,&nbsp;&nbsp;&nbsp;&nbsp;=20
os,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
ip,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
Connect_with,&nbsp;&nbsp; Customer<BR>apollo,&nbsp;&nbsp; RedHat =
7.3,&nbsp;=20
10.100.5.100,&nbsp;&nbsp; =
Telnet,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
Frieslandbank<BR>hera,&nbsp;&nbsp;&nbsp;&nbsp; RedHat 7.2,&nbsp;=20
10.100.33.227,&nbsp;=20
SSH,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
PON<BR>juno,&nbsp;&nbsp;&nbsp;&nbsp; Solaris 8,&nbsp;&nbsp; =
10.100.6.41&nbsp;=20
,&nbsp; SSH ,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
SNV<BR>artemis,&nbsp; RedHat 7.3,&nbsp; 10.100.6.42&nbsp; ,&nbsp;=20
Telnet,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
BLA</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D203123114-18112006></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D203123114-18112006>And =
wanted from=20
the&nbsp; "machines.txt"&nbsp;a .html file from it with headers in the =
order=20
that they are in the machines.txt file. </SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D203123114-18112006></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D203123114-18112006>Wrote =
the next code=20
to do so because I could not get at the headers ( name , os , ip ... ) =
in the=20
order they are in the machines.txt file. ( machines.keys gave nothing =
back ,=20
machines.0.key gives them back in random order , yes it's a hash. )=20
</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D203123114-18112006></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D203123114-18112006></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2><SPAN =
class=3D203123114-18112006>Template file :=20
machines.tt</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D203123114-18112006>=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D</SPAN></FONT></DIV>
<DIV><FONT face=3DCourier size=3D2><SPAN class=3D203123114-18112006>[% =
USE machines =3D=20
datafile('machines.txt',&nbsp; delim =3D ',') %]<BR>[% PERL %]<BR>my=20
@lables=3D();<BR>my $headers;<BR>&nbsp;&nbsp; open ( TXT , "&lt; =
machines.txt " )=20
or die "no such file"&nbsp; ;<BR>&nbsp;&nbsp; =
@lables=3Dsplit(/\s*,\s*/,readline (=20
TXT ));&nbsp; # assume headers are on the first line<BR>&nbsp; close=20
TXT;&nbsp;<BR>&nbsp; chomp $lables[$#lables];<BR>&nbsp; # export to the=20
namespace of Template ?, it , seems to do so<BR>&nbsp; $stash-&gt;set( =
headers=20
=3D&gt; \@lables );&nbsp;&nbsp; &nbsp;<BR>[% END =
%]<BR></SPAN></FONT></DIV>
<DIV><FONT face=3DCourier size=3D2><SPAN=20
class=3D203123114-18112006>&lt;HTML&gt;&lt;BODY&gt;<BR>&lt;Table =
border=3D1=20
cellpadding=3D0 cellspacing=3D0=20
STYLE=3D"font-family:Arial;font-size:9px"&gt;<BR>&lt;TR&gt;<BR>[% =
FOREACH lable IN=20
headers -%]<BR>&lt;TH bgcolor=3D"yellow"&gt;[% lable %]&lt;/TH&gt;<BR>[% =
END=20
-%]<BR>&lt;/TR&gt;<BR>[% FOREACH machine IN machines =
-%]<BR>&lt;TR&gt;<BR>[%-=20
FOREACH lable IN headers %]<BR>[% IF machine.$lable =3D=3D ""=20
%]<BR>&nbsp;&lt;TD&gt;&amp;nbsp&lt;/TD&gt;<BR>[%- ELSE =
-%]<BR>&lt;TD&gt;[%=20
machine.$lable %]&lt;/TD&gt;<BR>[%- END %]<BR>[%- END =
%]</SPAN></FONT></DIV>
<DIV><FONT face=3DCourier size=3D2><SPAN=20
class=3D203123114-18112006></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=3DCourier size=3D2><SPAN=20
class=3D203123114-18112006></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=3DCourier size=3D2><SPAN class=3D203123114-18112006>Now =
I must run it=20
: tpage --eval_perl machines.tt</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D203123114-18112006>Is =
there a more=20
elegant way to get at the headers in order they are in the machines.txt. =

</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D203123114-18112006></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D203123114-18112006>Thanks =
in advance :=20
Arno Teunisse</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D203123114-18112006></SPAN></FONT>&nbsp;</DIV></BODY></HTML>

------=_NextPart_000_0002_01C70B28.22907B80--