【问题标题】:Building a Table From perl Hash with colspan使用 colspan 从 perl 哈希构建表
【发布时间】:2013-01-04 03:15:29
【问题描述】:

我有一个类似于

的哈希
 $hash{$kayA}{$keyB}{val=>$value};

我需要将它放入一个 html 表中,其中包含 $keyA 的 TD 需要基于 $keyB 处的键数的行跨度

所以输出可能看起来像

<html>
<body>
<table border='1'>
  <tr><th colspan='2' >Col A</th><th>Col B</th><th>Value</th></tr>
  <tr>
  <td rowspan='2'><input name='myButton' type= "radio" id ="R1"></td>
  <td rowspan='2'> this is first kay</td>
  <td> this is second key 1</td><td>Value 1</td>
  </tr>
  <tr>
  <td>this is second key 2</td><td>Value 2</td>
  </tr>
</table>
</body>
</html>

就我的 perl 脚本而言,我正在努力解决如何将 tr 放在正确的位置

#!/usr/bin/perl

$hash{$kayA}{$keyB}{val=>$value};
my $TabPrt = "<table><tr><th>Col A></th><th>Col B</th><th>Value</th></tr>";
for my $keyA (sort keys %hash)
  {
   my $Row = scaler keys %{$hash}{kayA};
   $Row = "rowspan='$Row'";

   $TabPrt = $TabPrt. "<tr>  <td><input name='myButton' type= "radio" id ="R1"></td><td $Row></td>";
   for my $keyB (sort keys %{$hash}{$keyA}
    {
      my $val = hash{$kayA}{$keyB}{val};
      $TabPrt = $TabPrt . " <td>$keyB</td><td>$val</td>"
    }
  }

 $TabPrt = $TabPrt . "</tr></table>";  

【问题讨论】:

  • use strict;use warnings; 会告诉您“缩放器”是一个错字,而不是预期的“标量”。
  • Thanx Typo 抛开逻辑如何工作,将 & 放在哪里?
  • 在 Perl 中编写如此长的深度模板结构非常烦人。要么编写您自己的模板系统,使用自定义 DSL,要么停止编写复杂的报告并使用更多面向对象的方法。 TMTOWTDI 从未认为硬程序化色情是最好的解决方案。

标签: perl


【解决方案1】:

我对你的数据结构和代码都不太了解。

$hash{$kayA}{$keyB}{val=&gt;$value}; 可以编译,但在 Perl 中没有实际意义。

另外,这条线有问题:

$TabPrt = $TabPrt. "<tr>  <td><input name='myButton' type= "radio" id ="R1"></td><td $Row></td>";

它不会编译,因为字符串 "&lt;tr&gt; &lt;td&gt;&lt;input name='myButton' type= "radio 之前终止。我想你的意思是

$TabPrt .= q(<tr><td><input name="myButton" type="radio" id ="R1"></td><td>$Row</td>);

使用q()qq()(插值)引号运算符来引用包含'" 字符的字符串。

我假设您希望您的表格呈现为

+----------------------------------+------------------------+---------+
| Col A                            | Col B                  | Value   |
+==========+=======================+========================+=========+
| o Button | this is the first key | this is the second key | Value 1 |
|          |                       +------------------------+---------+
|          |                       | this is the second key | Value 2 |
+----------+-----------------------+------------------------+---------+

现在让我们假设你的哈希看起来像

my %hash = (
    key1 => { A => "val1", B => "val2" },
    key2 => { C => "val1", D => "val2" },
);

然后我们可以遍历这个哈希并构造 HTML:

sub make_table_corpus {
    my ($hash) = @_;
    my $html = "";
    for my $key (sort keys %$hash) {
        my $sub_hash = $hash->{$key};
        # first: how many rows will this key span?
        my $rowspan = keys %$sub_hash;
        # then: prepare all the secondary keys. Will return HTML fragments:
        my @secondary = prep_secondary_keys($sub_hash);
        $html .= html("tr", {},
            html("td", {rowspan => $rowspan}, " o Button "),
            html("td", {rowspan => $rowspan}, $key),
            # put the first secondary key here
            shift @secondary,
        );
        # append the other secondary keys:
        $html .= html("tr", {}, $_) for @secondary;
    }
    return $html;
}

# emits html fragments of key-value pairs, as <td> cells.
sub prep_secondary_keys {
    my ($hash) = @_;
    map { html("td", {}, $_) . html("td", {}, $hash->{$_}) }
        sort keys %$hash;
}

# creates a html fragment
sub html {
    my ($name, $attr, @childs) = @_;
    my $attrstring = "";
    while (my ($attname, $value) = each %$attr) {
        $value =~ s/"/&quot;/g;
        $attrstring .= qq( $attname="$value");
    }
    return join "", qq(<$name$attrstring>), @childs, qq(</$name>);
}

然后:

print make_table_corpus(\%hash);

使用上面的哈希,这将产生类似的输出

<tr>
  <td rowspan="2"> o Button </td>
  <td rowspan="2">key1</td>
  <td>A</td>
  <td>val1</td>
</tr>
<tr>
  <td>B</td>
  <td>val2</td>
</tr>
<tr>
  <td rowspan="2"> o Button </td>
  <td rowspan="2">key2</td>
  <td>C</td>
  <td>val1</td>
</tr>
<tr>
  <td>D</td>
  <td>val2</td>
</tr>

(当然不是故意的)

我做的不一样

  1. 我没有犯语法错误(use strict; use warnings 收到有关错误和错误的警告)
  2. 辅助键由外部子程序处理。这样,我们就可以简单地将第一个 HTML 片段放入第一行。
  3. 我编写了html sub 以避免在我的源代码中出现过多的引用问题。虽然这并不能替代模板系统,但它让生活变得更轻松,并为错误引入了单点故障,从而更容易解决问题。

扩展解决方案以打印出表格标题,并生成有效的 HTML 表格是一个简单的步骤。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-05
    • 2015-03-26
    • 2013-12-20
    • 2013-01-08
    • 2011-04-20
    • 2011-09-05
    • 2011-05-08
    • 1970-01-01
    相关资源
    最近更新 更多