Reputation: 3535
I have a hash in perl whose keys are domain names and value is reference to array of blacklisted zones in which the domain is blacklisted.Currently I am checking the domain against 4 zones.If the domain is blacklisted in the particular zone the I push the zone names in the array.
domain1=>(zone1,zone2)
domain2=>(zone1)
domain3=>(zone3,zone4)
domain4=>(zone1,zone2,zone3,zone4)
I want to create a HTML table from these values in CGI like
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
I tried it using map in CGI like
print $q->tbody($q->Tr([
$q->td([
map {
map{
$_
}'$_',@{$result{$_}}
}keys %result
])
)
I am unable to the desired output.I am not sure of using if-else in map. If I manually generate the td's Then I need to write a separate td's for each condition like
If(zone1&&zone2&&!zone3&&!zone4){
print "<td>true</td><td>true</td><td><false/td><td>false</td>";
}
......
It is very tedious.How can I get that output?
Upvotes: 0
Views: 599
Reputation: 35198
Convert your Hash of Arrays to a Hash of Hashes. This makes it easier to test for existence of a particular zone.
The following demonstrates and then displays the data in a simple text table:
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;
}
Outputs:
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
Upvotes: 1