有人能告诉我@{hash{$key}}
是什么意思?当我写下@{hash{$key}}[$i]=$list[$i+1]
时,我是否正确?
0
A
回答
7
@{hash{$key}}
是写
@hash{$key}
这就是所谓的散片奇怪的是。它允许您一次从散列获取多个值。例如,
say for @hash{'a', 'b'};
意味着
say for $hash{a}, $hash{b};
所以这是相当愚蠢的使用
@hash{$key}
,而不是
$hash{$key}
@{hash{$key}}[$i]=$list[$i+1];
不能编译,所以不行,使用它是不正确的。
3
正如ikegami所解释的,@{hash{$key}}
是一个散列片(只有一个元素)。
这很可能是您想要的:@{$hash{$key}}
。如果散列的每个元素都是数组引用,则此语法将允许您访问该数组。
例如,假设你的散列包含数组,像这样:
my %hash = (
foo => [1, 2, 3, 4, 5],
bar => [qw/baz sproing blargh/]
);
@{$hash{foo}}
将(1, 2, 3, 4, 5)
如果是这样的话,那么这句法会工作:
${$hash{$key}}[$i]=$list[$i+1]
请注意,您只访问数组的一个元素,而不是整个数组。因此,您使用${$hash{$key}}
而不是@{$hash{$key}}
。
做同样的事情的另一种方法是这样的:
$hash{$key}->[$i]=$list[$i+1]
的->
语法提领往往是最好${...}
,因为它是清洁的,尤其是当你有一个复杂的数据结构。
相关问题
- 1. 为什么:在Ruby中key.hash!='key'.hash?
- 2. 在Perl中,if(%hash)和if(定义的%hash)之间有什么区别?
- 3. {}在perl中的含义是什么?
- 4. 在Perl中=〜的含义是什么?
- 5. @_ -1在Perl中的含义是什么?
- 6. perl中()的含义是什么?
- 7. Perl中标题的含义是什么?
- 8. perl中$#的含义是什么?
- 9. Perl中“锁定”的含义是什么?
- 10. Perl中'at'(@@)的含义是什么?
- 11. 在Android上使用Facebook API的Key Hash的目的是什么?
- 12. $ this - > {$ key}在PHP中的含义是什么?
- 13. 是什么含义 - >和Perl中
- 14. Hash Map Key比较
- 15. array vs hash key search
- 16. psycopg连接池中'key'的含义是什么?
- 17. 为什么一些条目在Perl中不存在ENV hash
- 18. Perl中@array - > [4]或%hash - > {key}行为的文档在哪里?
- 19. <DATA>在Perl错误消息中的含义是什么?
- 20. 什么意思 - >在Perl 6中的含义?
- 21. LOC和ACC在perl中的含义是什么?
- 22. <*>在Perl中的含义是什么?
- 23. $#数组在Perl中的含义是什么?
- 24. 反斜杠'\'在perl语言中的含义是什么?
- 25. s {SOMESTR} {$ myvar}在perl中的含义是什么?
- 26. 什么是“$ 0 =〜s!。*/!!”在perl中的含义?
- 27. 这些空数组赋值在perl中的含义是什么?
- 28. if(@array)在perl中的含义是什么?
- 29. ENDOFTEXT在此Perl代码中的含义是什么?
- 30. 在Perl中,if(s/^ \ + //)的含义是什么?