使用colspan从perl哈希构建表
我有一个类似于使用colspan从perl哈希构建表,perl,Perl,我有一个类似于 $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&l
$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>
Col ACol B值
这是第一个凯
这是第二个键1值1
这是第二个键2的值2
这就是我的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>";
#/usr/bin/perl
$hash{$kayA}{$keyB}{val=>$value};
my$TabPrt=“Col A>Col B值”;
对于我的$keyA(排序键%hash)
{
my$Row=缩放键%{$hash}{kayA};
$Row=“rowspan=”$Row';
$TabPrt=$TabPrt.“;
对于我的$keyB(排序键%{$hash}{$keyA}
{
my$val=hash{$kayA}{$keyB}{val};
$TabPrt=$TabPrt.“$keyB$val”
}
}
$TabPrt=$TabPrt.“;
我不太了解您的数据结构和代码
这个$hash{$kayA}{$keyB}{val=>$value};
可以编译,但在Perl中没有实际意义
此外,此线路有故障:
$TabPrt = $TabPrt. "<tr> <td><input name='myButton' type= "radio" id ="R1"></td><td $Row></td>";
使用q()
我假设您希望您的表呈现为
+----------------------------------+------------------------+---------+
| 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/"/"/g;
$attrstring .= qq( $attname="$value");
}
return join "", qq(<$name$attrstring>), @childs, qq(</$name>);
}
使用上面的散列,这将产生如下输出
<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>
o按钮
关键1
A.
瓦尔1
B
瓦尔2
o按钮
键2
C
瓦尔1
D
瓦尔2
(当然,没有意图)
我做了什么不同
我没有出现语法错误(使用strict;使用warnings
获得有关错误和错误的警告)
辅助键由外部子例程处理。这样,我们可以简单地获取第一个HTML片段,并将其轻松地放入第一行
我编写了html
sub以避免在源代码中出现过多的引用问题。虽然这并不能替代模板系统,但它使生活变得更轻松,并引入了单点错误,从而使问题更容易修复
扩展解决方案以打印出表格标题,并生成有效的HTML表格从这里开始是一个简单的步骤。使用严格;
和使用警告;
将向您显示“scaler”是一个输入错误,而不是预期的“scalar”“.Thanx打字错误撇开逻辑在何处放置&?用Perl编写如此长的深度模板构造非常烦人。要么编写自己的模板系统,使用自定义DSL,要么停止编写复杂的报告,使用更面向对象的方法。TMTOWTDI从未认为硬编程色情是最佳解决方案。
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>