2014-11-03 144 views
1

我已經在這幾天沒有成功。我有兩個維度的散列,我希望創建一個表。Perl多維哈希表漂亮表

這裏是哈希轉儲的例子:

$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> 

這裏是我的代碼,找出所有%認購的價值:雖然每次我打印一個輸入框,我打印的新錶行,我知道我迷路

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(); 

在我的循環中。

例子:

... 
<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> 

我怎樣才能做到這一段?有沒有更好的方法去解決它?

+0

你想確定命令(Bill,Hillary,George,Laura)? – ysth 2014-11-03 23:55:58

+0

按字母順序將是首選。除非你的意思完全不同。 – 2014-11-03 23:57:47

回答

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();