【问题标题】:How Create Html Table from formatted text file如何从格式化的文本文件创建 Html 表
【发布时间】:2013-04-24 13:33:01
【问题描述】:

作为主题,我必须编写一个转换日志文件的 perl 脚本,例如:

pippo   2   64  0   2
pippo_a 3   24  0   2
Pippo_b 2   60  0   23
pluto   0   18  0   4
pluto_a 8   25  0   6

paperino    0   11  1   7
paperino_a  0   27  0   10
coyote  0   29  0   7
beepbeep    0   1   0   0
tommy   3   31  0   27
paperone    4   43  1   15
paperone_a  4   52  0   13
benjamin    0   21  1   35

paperina    10  0   0   0
papera  0   0   0   0
quiquoqua   3   26  0   17
quiquoqua_a 3   25  0   3
pochaontas  0   12  0   68

minnie  11  60  3   384

在这个html表格格式中:

链接:http://imageshack.us/a/img90/7238/tabellao.jpg

现在我已经编写了这个小 perl 脚本,但我无法从 @cells 数组创建写入周期:

#! /usr/bin/perl
print "Content-type:text/html\r\n\r\n";
use CGI qw(:standard);
use strict;
use warnings;
use DateTime;

my $line;
my $file;


$file='loggi.txt';
open(F,$file)||die("Could not open $file");


print "<html>\n
<style type='text/css'>\n
body {\n
font-family: Verdana, Arial, Helvetica, sans-serif;\n
color: #333;\n
}\n
table {\n
font-size:11px;\n
}\n
td#vtl, td#pool {\n
font-weight: bold;\n
writing-mode: bt-rl;\n
#-webkit-transform: rotate(90deg);\n
#-moz-transform: rotate(90deg);\n
#-ms-transform: rotate(90deg);\n
#-o-transform: rotate(90deg);\n
#transform: rotate(90deg);\n
}\n
.zone tr{\n
border:2px dotted black;\n
}\n
</style>\n
<body>\n
<table border=2>\n
<!--Intestazione-->\n
<tr>\n
    <th id='vtl'>houses</th>\n
    <th id='pool'>id</th>\n
    <th id='host'>name</th>\n
    <th id='vergini'>Ver.(*)</td>\n
    <th id='riciclabili'>(yes)</th>\n
    <th id='vuote'>zero</th>\n
    <th id='full'>Full</th>\n
    <th id='spazi'>space</th>\n
</tr>\n
<!--Corpo-->\n
<!--VTLA-->";
print "<tr align='center'>\n
    <td id='case' rowspan=7>casaa</td>\n
    <td id='id' rowspan=7>10</td>\n
    <th id='a' colspan=5>pippi</th>\n
    <td id='space' rowspan=30>0</td>\n
</tr>";

while ($line=<F>)
{ 
    print "<tr>";
    my @cells= split '  ',$line;
    foreach my $cell (@cells)
    {
        if $cell == 'pippo' {
       print "<td id='name'>$cell</td>";
       print "<td id='a'>$cell</td>";
       print "<td id='b'>$cell</td>";
       print "<td id='c'>$cell</td>";
       print "<td id='d'>$cell</td>";
    }}
    print "</tr>";
}
close(F);
print "</table>\n
<br/>\n
Situazione aggiornata al".DateTime->now()->strftime("%a, %d %b %Y %H:%M:%S %z")."
</body>\n
</html>";

这是我的第一个长 perl 脚本,有人可以帮我推理吗?

【问题讨论】:

    标签: html perl html-table matching


    【解决方案1】:

    您可以在此处进行一些快速改进

    打开文件时使用 3 参数 open 更安全,不会意外打印到文件中

    open(my $fh, '<', $file) or die("Could not open $file");
    

    多行打印语句有点难以阅读。使用 here-doc 来定义多行字符串是很常见的做法

    print <<"EOT";
    <html>\n
    <style type='text/css'>\n
    body {\n
    font-family: Verdana, Arial, Helvetica, sans-serif;\n
    color: #333;\n
    }\n
    table {\n
    font-size:11px;\n
    }\n
    td#vtl, td#pool {\n
    font-weight: bold;\n
    writing-mode: bt-rl;\n
    etc etc etc
    EOT
    

    说实话,您应该考虑使用 Template Toolkit 或其他模板引擎。

    在 Perl 中,您使用 eq 运算符来测试字符串等价性,所以我认为这个

    if $cell == 'pippo' {
    

    应该是这样的

    if($cell eq 'pippo') {
    

    最后,在脚本中很常见的是,您逐行解析文件以使用 chomp,这会从您刚刚阅读的行中去除尾随换行符,例如

    while ($line=<F>)
    { 
        chomp($line); #otherwise $line ends in \n
    

    希望这足以让您入门,祝您好运!

    【讨论】:

      【解决方案2】:
      use strict;
      use warnings;
      
      use CGI qw(:all);
      
      my $cgi = new CGI ;
      
      my @header_array = ('<Header1>',
                          '<Header2>',
                          '<Header3>',
                          '<Header4>',
                          );
      
      open (my $HTML, ">","index.html") or die $!; 
      print $HTML $cgi->start_html("<Title>");
      print $HTML "<link rel='stylesheet' href='<Path to css>' type='text/css' media='screen' />";
      
      print $HTML $cgi->start_table({-border=>1, -cellspacing=>2, -cellpadding=>1});
      
      print $HTML $cgi->Tr({-align=>'center',-valign=>'middle'},[$cgi->th(\@header_array)]);
      
      open (my $LOG, "<","log.txt") or die $!;
      foreach my $line (<$LOG>){
          my @row =  split("\t",$line);
          print $HTML $cgi->Tr({-align=>'center',-valign=>'middle'},[$cgi->th(\@row)]);
      }
      close ($LOG);    
      print $HTML $cgi->end_table();
      print $HTML $cgi->end_html();
      close ($HTML);    
      

      您可以更改打印到 html 文件的方式以满足您的特定需求。

      【讨论】:

        猜你喜欢
        • 2015-02-14
        • 1970-01-01
        • 2020-10-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-06
        • 1970-01-01
        相关资源
        最近更新 更多