就看懂一段perl代码

发布于 2024-12-21 07:52:22 字数 463 浏览 0 评论 0原文

对于以下代码段,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

是伱的 2024-12-28 07:52:22

如果您将嵌套数据结构抽象出来,这会有所帮助:

my $VectorHashRef = $dat1->{dat1}->[$Index2]->{Vector};
foreach my $Index1 (sort { $A <=> $B } keys %$VectorHashRef ) {
    # This should be corrected to $a <=> $b - names are case sensitive
    if ($VectorHashRef->{$Index1} == 2.0) { 
         next;
    }
    printf $sth;
}

$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:

my $VectorHashRef = $dat1->{dat1}->[$Index2]->{Vector};
foreach my $Index1 (sort { $A <=> $B } keys %$VectorHashRef ) {
    # This should be corrected to $a <=> $b - names are case sensitive
    if ($VectorHashRef->{$Index1} == 2.0) { 
         next;
    }
    printf $sth;
}

$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 of EXPRESSION.

  • 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 of EXPRESSION.

一笑百媚生 2024-12-28 07:52:22

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 the sort command before the comparison is executed.

It is time to compile the script with use warnings; and use strict;.

时光暖心i 2024-12-28 07:52:22

$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 (see sort 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 - lowercased

The resulting list of sorted keys is being iterated through and assigned to $Index1.

If the value of the $Index1 key in Vector is 2.0, then the key is skipped.

任谁 2024-12-28 07:52:22
  • 循环散列键排序数组的 my 范围的 $Index1 变量,存储在散列的 Vector 中,存储在 放入 $dat1 中的列表的 $Index2 位于 dat1 处。
  • 检查同一个哈希中的 $Index1 是否等于 2.0

这听起来可能很复杂,任何人都可以尝试将其翻译成人类语言,但没有人能为你理解它,这需要一些努力你的身边。

  • Loop my-scoped $Index1 variable of the sorted array of keys of the hash, stored in Vector of the hash, stored at $Index2 of the list put into $dat1 has at dat1.
  • check if the $Index1 in the same hash is equal to 2.0

it 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文