Perl多维哈希到漂亮的表

时间:2014-11-03 23:53:57

标签: perl multidimensional-array hashtable

我已经在这几天了,但没有成功。我有两个维度的哈希,我希望创建一个表格。

以下是散列转储的示例:

$VAR1 = {
      'bill' => {
                   'deploy_status' => 'checked',
                   'bill_acceptance' => 'checked',
                   'daily_status' => '',
                   'awareness' => 'checked',
                   'aar_summary' => 'checked',
                   'daily_snapshot' => ''
                 },
      'hillary' => {
                      'deploy_status' => 'checked',
                      'bill_acceptance' => 'checked',
                      'daily_status' => 'checked',
                      'awareness' => 'checked',
                      'aar_summary' => '',
                      'daily_snapshot' => 'checked'
                    },
      'george' => {
                   'deploy_status' => 'checked',
                   'bill_acceptance' => 'checked',
                   'daily_status' => '',
                   'awareness' => 'checked',
                   'aar_summary' => 'checked',
                   'daily_snapshot' => 'checked'
                 },
      'laura' => {
                        'deploy_status' => '',
                        'bill_acceptance' => 'checked',
                        'daily_status' => 'checked',
                        'awareness' => '',
                        'aar_summary' => 'checked',
                        'daily_snapshot' => ''
                      }
    };

您可能已经注意到,最后定义的值最终将表示是否选中了输入复选框。

我的最终数据应该如下所示..将完成所有模板的辅助哈希键。

<div class="user-table add-bottom sortable" >
    <div class="user-table-header">
        <div class="user-table-cell"> &nbsp; </div>
        <div class="user-table-cell">Bill</div>
        <div class="user-table-cell">Hillary</div>
        <div class="user-table-cell">George</div>
        <div class="user-table-cell">Laura</div>
    </div>

    <div class="user-table-row">
        <div class="user-table-cell">AAR Summary</div>
        <div class="user-table-cell"> <input type="checkbox" name="bill-aar_summary" checked> </div>
        <div class="user-table-cell"> <input type="checkbox" name="hillary-aar_summary"> </div>
        <div class="user-table-cell"> <input type="checkbox" name="george-aar_summary" checked> </div>
        <div class="user-table-cell"> <input type="checkbox" name="laura-aar_summary" checked> </div>
    </div>
    <div class="user-table-row">
        <div class="user-table-cell">Daily Snapshot</div>
        <div class="user-table-cell"> <input type="checkbox" name="bill-daily_snapshot"> </div>
        <div class="user-table-cell"> <input type="checkbox" name="hillary-daily_snapshot" checked> </div>
        <div class="user-table-cell"> <input type="checkbox" name="george-daily_snapshot" checked> </div>
        <div class="user-table-cell"> <input type="checkbox" name="laura-daily_snapshot"> </div>
    </div>
</div>

这是我的代码,用于查找%subscription的所有值:

print $html->tableOpen();
print $html->tableHeaderOpen();

foreach my $friend(sort keys %subscription) {

    if ( $fCount == $lastFriend ) {
        print $html->tableHead($friend); print $html->tableHeadClose;
    }
    else { print $html->tableHead($friend); } 

    foreach my $template (sort keys %{ $subscription{$friend}}) {       
        print $html->tableRowStart();

        print $html->tableCellOpen . $html->checkBox($subscription{$friend}{$template}); . $html->tableCellClose();

        print $html->tableRowClose();
    }
}

print $html->tableClose();

虽然每次我打印一个输入框时,我都会打印一个新的表格行,我知道我在for循环中迷路了。

示例:

...
<div class="user-table-row">
    <div class="user-table-cell">Daily Snapshot</div>
    <div class="user-table-cell"> <input type="checkbox" name="bill-daily_snapshot"> </div>
</div>
...
<div class="user-table-row">
    <div class="user-table-cell">Daily Snapshot</div>
    <div class="user-table-cell"> <input type="checkbox" name="hillary-daily_snapshot"> </div>
</div>

我怎样才能完成这个时期?有没有更好的方法呢?

1 个答案:

答案 0 :(得分:0)

你需要像这样嵌套你的循环(免责声明:未经测试!)。我还会添加另一个哈希来存储模板的友好名称:

my %key_names = {
    'deploy_status' => 'Deploy Status',
    'bill_acceptance' => 'Bill Acceptance',
    'daily_status' => 'Daily Status',
    'awareness' => 'Awareness',
    'aar_summary' => 'AAR summary',
    'daily_snapshot' => 'Daily Snapshot'
};

print $html->tableOpen();

# Header
print $html->tableHeaderOpen();
foreach my $friend(sort keys %subscription) {
    print $html->tableHead($friend);
}
print $html->tableHeadClose;

# Rows
foreach my $template (sort keys %key_names) {       
    print $html->tableRowStart();
    print $html->tableCell($key_names{$template});
    foreach my $friend(sort keys %subscription) {
        print $html->tableCellOpen . $html->checkBox($subscription{$friend}{$template}); . $html->tableCellClose();
    }
    print $html->tableRowClose();
}

print $html->tableClose();