Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl:为单个键打印具有多个值的哈希_Perl_Hash_Reference - Fatal编程技术网

Perl:为单个键打印具有多个值的哈希

Perl:为单个键打印具有多个值的哈希,perl,hash,reference,Perl,Hash,Reference,我对一个键使用了多个值的散列。如何在散列中独立打印多个密钥值 # 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>&

我对一个键使用了多个值的散列。如何在散列中独立打印多个密钥值

# 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

尝试将循环的第二行替换为

print "<td>$_</td>" for @{ $hash{$key} };
为@{$hash{$key}打印“$\”;

它将循环数组引用中的每个项,并将它们包装在
td
标记中。

使用另一个
foreach
循环获取值。或者使用
map
将它们包装在
中并打印结果。