请检查以下代码。我想打印数组,但它只打印数组中的第一项。
$prefix = 'ABC';
$search_pc_exclude = "PC1 PC2 PC3";
@exclude = split(/\s+/, $search_pc_exclude);
push @prefix, {"pre" => $prefix, "exc" => @exclude};
print $prefix[0]->{pre};
print $prefix[0]->{exc}; #why this is not array?
答案 0 :(得分:3)
分配实际上是这样处理的:
push @prefix, {"pre" => $prefix, "exc" => "PC1", "PC2" => "PC"}
这为您提供了这些键的哈希值。你需要一个数组引用:
# This creates a copy of @exclude
push @prefix, {"pre" => $prefix, "exc" => [@exclude]}
或者:
# This creates a reference to @exclude. Any modifications to
# $prefix[0]->{exc} are actually modifications to @exclude
push @prefix, {"pre" => $prefix, "exc" => \@exclude}