【发布时间】:2014-09-19 18:17:31
【问题描述】:
我有一个键的多个值的散列。如何独立打印哈希中的多个键值?
# HASH with multiple values for each key
my %hash = ( "fruits" => [ "apple" , "mango" ], "vegs" => ["Potato" , "Onion"]);
# SET UP THE TABLE
print "<table border='1'>";
print "<th>Category</th><th>value1</th><th>value2</th>";
#Print key and values in hash in tabular format
foreach $key (sort keys %hash) {
print "<tr><td>".$key."</td>";
print "<td>".@{$hash{$key}}."</td>";
}
* 当前输出:*
Category Value1 Value2
fruits apple mango
vegs Potato Onion
* 所需输出:*
Category Value1 Value2
fruits apple mango
vegs Potato Onion
【问题讨论】:
-
对值使用另一个
foreach循环。或者使用map将它们包装在<td>...</td>中并打印该结果。