就看懂一段perl代码
对于以下代码段,
foreach my $Index1 (sort { $A <=> $B } keys %{$dat1->{dat1}->[$Index2]->{Vector}})
{
if($dat1->{dat1}->[$Index2]->{Vector}->{$Index1} == 2.0) { next }
printf $sth
}
如何理解 my $Index1 (sort { $A <=> $B } keys %{$dat1->{dat1}->[$Index2]- >{Vector}})
和($dat1->{dat1}->[$Index2]->{Vector}->{$Index1} == 2.0)
如何一块一块理解它们的底层逻辑?谢谢。
With respect to the following code segment,
foreach my $Index1 (sort { $A <=> $B } keys %{$dat1->{dat1}->[$Index2]->{Vector}})
{
if($dat1->{dat1}->[$Index2]->{Vector}->{$Index1} == 2.0) { next }
printf $sth
}
How to understand the my $Index1 (sort { $A <=> $B } keys %{$dat1->{dat1}->[$Index2]->{Vector}})
and ($dat1->{dat1}->[$Index2]->{Vector}->{$Index1} == 2.0)
How to understand their underlying logic piece by piece? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您将嵌套数据结构抽象出来,这会有所帮助:
$dat1->{dat1}->[$Index2]->{Vector}
是访问某些数据(a hashref)位于嵌套数据结构的深处。它采用 hashref$dat1
,访问哈希键"dat1"
指向的数据结构 - 并且该键指向数组引用。您可以从带有索引$Index2
的数组中获取值 - 该值是对下一个数据结构 (hashref) 的引用。您将该哈希值作为键"Vector"
- 这是另一个哈希引用。现在,让我们使用
$VectorHashRef
变量为 Vector hashref 起别名(我宽松地使用了别名一词)。现在,您正在迭代该 hashref 的键,按数字排序(有关其工作原理的详细信息,请参阅 Dan 的答案),并且对于每个键,将哈希值与
2.0
进行比较,仅在满足以下条件时才打印某些内容:值不是 2.0。为了更好地理解这一点,您需要阅读一些 Perl 数据结构教程 - 例如 Data Structures Cookbook (又名
perldoc perldsc
)简而言之:
当您看到类似
EXPRESSION->{KEY_EXPRESSION}
的内容时,这意味着要访问该值(对于 KEY_EXPRESSION 指定的键)在哈希引用中,哈希引用是EXPRESSION
的结果。当您看到类似
EXPRESSION->[INDEX_EXPRESSION]
的内容时,这意味着访问数组引用中的值(对于 INDEX_EXPRESSION 指定的数组下标),数组引用是EXPRESSION
的结果。It helps if you abstract the nested data structure away:
$dat1->{dat1}->[$Index2]->{Vector}
is a way to access some piece of data (a hashref) deep in a nested data structure. It takes a hashref$dat1
, accesses the data structure that is pointed to by a hash key"dat1"
- and that key points to an array reference. You take the value from that array with an index$Index2
- and that value is a reference to the next data structure (hashref). You take the value of that hash for the key"Vector"
- which is another hashref.Now, let's alias that Vector hashref with
$VectorHashRef
variable (I'm using the word alias loosely).Now, you are iterating over the keys of that hashref, sorted numerically (see Dan's answer for details on how that works), and for each key, comparing the hash value to
2.0
, printing something only if the value is NOT 2.0.To understand this well, you need to read somee data structures in Perl tutorial - for example Data Structures Cookbook (aka
perldoc perldsc
)In short:
When you see something that looks likie
EXPRESSION->{KEY_EXPRESSION}
, that means accessing the value (for a key specified by KEY_EXPRESSION) in a hashref, with the hash reference being the result ofEXPRESSION
.When you see something that looks likie
EXPRESSION->[INDEX_EXPRESSION]
, that means accessing the value (for a array subscript specified by INDEX_EXPRESSION) in an array reference, with the array reference being the result ofEXPRESSION
.sort
使用的变量是小写的$a
和$b
;该代码将无法正常工作,因为在执行比较之前sort
命令未设置变量$A
和$B
。现在是使用
use warnings;
和use strict;
编译脚本的时候了。The variables used by
sort
are$a
and$b
in lower case; that code will not work properly because variables$A
and$B
are not set by thesort
command before the comparison is executed.It is time to compile the script with
use warnings;
anduse strict;
.$dat1->{dat1}->[$Index2]->{Vector}
是一个哈希引用,通过将其放入%{}
来取消引用。正在使用
keys
检索该哈希的密钥。这些键使用块中的典型
cmp
/<=>
运算符进行排序 (请参阅此处的sort
文档)。 请注意,您的排序代码实际上已损坏 - 正如链接的文档中所述,排序块中使用的特殊变量必须是$a
和$b
- 小写< /strong>排序后的键列表将被迭代并分配给
$Index1
。如果
Vector
中$Index1
键的值为2.0,则跳过该键。$dat1->{dat1}->[$Index2]->{Vector}
is a hash reference, and it's dereferenced by putting it in%{}
.The keys are being retrieved for that hash with
keys
.Those keys are being sorted using a typical
cmp
/<=>
operator in a block (seesort
documentation here). Please note that your sorting code is actually broken - as noted in the documentation linked, special variables used in soriting's block must be$a
and$b
- lowercasedThe resulting list of sorted keys is being iterated through and assigned to
$Index1
.If the value of the
$Index1
key inVector
is 2.0, then the key is skipped.my
范围的$Index1
变量,存储在散列的Vector
中,存储在放入
位于$dat1
中的列表的 $Index2dat1
处。$Index1
是否等于 2.0这听起来可能很复杂,任何人都可以尝试将其翻译成人类语言,但没有人能为你理解它,这需要一些努力你的身边。
my
-scoped$Index1
variable of the sorted array of keys of the hash, stored inVector
of the hash, stored at$Index2
of the list put into$dat1
has atdat1
.$Index1
in the same hash is equal to 2.0it may sound complicated, anyone can try to translate it into human language, but no one can understand it for you and it will take some effort on your side.