2014-08-30 64 views
0

我在perl中有一個關鍵字是域名的哈希值,值是對列入黑名單的黑名單區域的數組的引用。目前我正在檢查4個區域的域名。如果域名被列入黑名單在特定區域中,我推入數組中的區域名稱。使用perl創建Html表格

domain1=>(zone1,zone2) 
domain2=>(zone1) 
domain3=>(zone3,zone4) 
domain4=>(zone1,zone2,zone3,zone4) 

我想創建從CGI這些值的HTML表像

domain-names zone1 zone2 zone3 zone4 

domain1   true  true false false 
domain2   true  false false false 
domain3   false false true true 
domain4   true  true true true 

我用它映射在CGI像

print $q->tbody($q->Tr([ 
          $q->td([ 
            map { 
             map{ 
              $_ 
              }'$_',@{$result{$_}} 
             }keys %result 
            ])   
) 

我無法所需的輸出試過。我不確定在地圖中使用if-else。 如果我手動生成TD的然後,我需要寫一個單獨的TD的像

If(zone1&&zone2&&!zone3&&!zone4){ 

    print "<td>true</td><td>true</td><td><false/td><td>false</td>"; 

    } 
    ...... 

每個條件這是非常tedious.How可我得到的輸出?

回答

1

將你的哈希數組轉換爲哈希哈希值。這使得更容易測試特定區域的存在。

下面演示,然後顯示在一個簡單的文本表中的數據:

use strict; 
use warnings; 

# Your Hash of Arrays 
my %HoA = (
    domain1 => [qw(zone1 zone2)], 
    domain2 => [qw(zone1)], 
    domain3 => [qw(zone3 zone4)], 
    domain4 => [qw(zone1 zone2 zone3 zone4)], 
); 

# Convert to a Hash of hashes - for easier testing of existance 
my %HoH; 
$HoH{$_} = { map { $_ => 1 } @{ $HoA{$_} } } for keys %HoA; 

# Format and Zone List 
my $fmt = "%-15s %-8s %-8s %-8s %-8s\n"; 
my @zones = qw(zone1 zone2 zone3 zone4); 

printf $fmt, 'domain-names', @zones; # Header 

for my $domain (sort keys %HoH) { 
    printf $fmt, $domain, map { $HoH{$domain}{$_} ? 'true' : 'false' } @zones; 
} 

輸出:

domain-names zone1 zone2 zone3 zone4 
domain1   true  true  false false 
domain2   true  false false false 
domain3   false false true  true  
domain4   true  true  true  true