开发者

Cannot print array in the hash of another array

开发者 https://www.devze.com 2023-03-26 08:44 出处:网络
Please check the following code. I want to print array, but it only print the first item in array. $prefix = \'ABC\';

Please check the following code. I want to print array, but it only print the first item in array.

$prefix = 'ABC';
$search_pc_exclude = "PC1 PC2 PC3";

@exclude = split(/\s+/, $search_pc_excl开发者_运维技巧ude);
push @prefix, {"pre" => $prefix, "exc" => @exclude};

print $prefix[0]->{pre};
print $prefix[0]->{exc}; #why this is not array?


The assignment actually is processed like this:

push @prefix, {"pre" => $prefix, "exc" => "PC1", "PC2" => "PC"}

Which gives you a hash with these keys. You need an array reference for that:

# This creates a copy of @exclude
push @prefix, {"pre" => $prefix, "exc" => [@exclude]}

Or:

# This creates a reference to @exclude. Any modifications to
# $prefix[0]->{exc} are actually modifications to @exclude
push @prefix, {"pre" => $prefix, "exc" => \@exclude}
0

精彩评论

暂无评论...
验证码 换一张
取 消