1

私は次のようなハッシュを持っています

 $hash{$kayA}{$keyB}{val=>$value};

これを html テーブルに出力する必要があります。TD を含む $keyA には、$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>";  
4

1 に答える 1

2

私はあなたのデータ構造もあなたのコードも完全には理解していません。

これ$hash{$kayA}{$keyB}{val=>$value};はコンパイルされますが、Perlでは実際の意味はありません。

また、この行に問題があります。

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

"<tr> <td><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ソースコードでの過度の引用の問題を回避するために、サブを作成しました。これはテンプレートシステムに代わるものではありませんが、作業が少し楽になり、ミスの単一障害点が発生するため、問題の修正が容易になります。

ソリューションを拡張してテーブルヘッダーを印刷し、有効なHTMLテーブルを作成することは、ここからの簡単な手順です。

于 2013-01-03T18:59:52.343 に答える