我如何将两个数组分配给Perl中的散列? [英] How can I assign two arrays to a hash in Perl?
问题描述
我有两行大的数组代码(所以不能将它写入哈希),我想用哈希连接。
例如, $ array1 [0]
成为键值, $ array2 [0]
成为值等等, C $ C> $ ARRAY1 [150] , $数组2 [150]
。
任何想法如何做到这一点? 您可以做它在一个单一的任务:
my%hash;
@hash {@ array1} = @ array2;
这是一个常见的习惯用语。从 perldoc perldata on slices :
< blockquote>
如果您对为什么在$ hash中使用
和@ b $ b而忽略$'b'的问题感到困惑,请将它想象成这样。
类型的括号(正方形或卷曲)
控制着它是一个数组还是一个
散列被查看。在另一个
手上,数组或$散列上的前导符号('$'或'@')
表示
是否获得奇异值
(一个标量)或复数形式(一个列表)。
当我看到其中一个时,我看到拉链的心理图像...
I have lines of code with two large arrays (so can't just write it into a hash) which I want to connect with a hash.
For example, $array1[0]
becomes the key and $array2[0]
becomes the value and so on to $array1[150]
,$array2[150]
.
Any ideas how I do this?
You can do it in a single assignment:
my %hash;
@hash{@array1} = @array2;
It's a common idiom. From perldoc perldata on slices:
If you're confused about why you use an '@' there on a hash slice instead of a '%', think of it like this. The type of bracket (square or curly) governs whether it's an array or a hash being looked at. On the other hand, the leading symbol ('$' or '@') on the array or hash indicates whether you are getting back a singular value (a scalar) or a plural one (a list).
When I see one of these I see a mental image of a zipper...
这篇关于我如何将两个数组分配给Perl中的散列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!