首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用colspan从perl Hash构建表

使用colspan从perl Hash构建表
EN

Stack Overflow用户
提问于 2013-01-04 01:38:45
回答 1查看 716关注 0票数 1

我有一个类似于

代码语言:javascript
复制
 $hash{$kayA}{$keyB}{val=>$value};

我需要把这个放到一个html表中,TD包含$keyA需要一个基于$keyB的键数的行距。

因此,输出可能如下所示

代码语言:javascript
复制
<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放在正确的位置而苦苦思索

代码语言:javascript
复制
#!/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>";  
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-04 02:59:52

我既不太了解您的数据结构,也不太了解您的代码。

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

另外,这一行是有问题的:

代码语言:javascript
复制
$TabPrt = $TabPrt. "<tr>  <td><input name='myButton' type= "radio" id ="R1"></td><td $Row></td>";

它不会编译,因为字符串"<tr> <td><input name='myButton' type= "正好在radio之前终止。我想你的意思是

代码语言:javascript
复制
$TabPrt .= q(<tr><td><input name="myButton" type="radio" id ="R1"></td><td>$Row</td>);

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

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

代码语言:javascript
复制
+----------------------------------+------------------------+---------+
| 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 |
+----------+-----------------------+------------------------+---------+

现在让我们假设您的散列如下所示

代码语言:javascript
复制
my %hash = (
    key1 => { A => "val1", B => "val2" },
    key2 => { C => "val1", D => "val2" },
);

然后,我们可以遍历这个散列并构造HTML:

代码语言:javascript
复制
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>);
}

然后:

代码语言:javascript
复制
print make_table_corpus(\%hash);

使用上面的散列,这将产生如下输出

代码语言:javascript
复制
<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>

(当然,没有刻意)

我所做的不同之处

mistakes)

  • The辅键是由一个外部的子例程处理的,而
  1. I没有做语法错误(use strict; use warnings是用来警告错误的)。这样,我们就可以很容易地将第一个HTML片段放到第一行。
  2. 我写了html sub,以避免在源代码中出现过多的引号问题。虽然这不能替代模板系统,但它使生活变得更简单,并为错误引入了单点故障,这使得问题更容易修复。

从这里开始,扩展解决方案以打印出表头并生成有效的HTML表是很简单的一步。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14144196

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档