gpt4 book ai didi

perl - "last"在 Perl 中做什么?

转载 作者:行者123 更新时间:2023-12-03 18:14:31 24 4
gpt4 key购买 nike

在下面的代码中,last 做了什么?在while循环中做什么?我明白,如果 $matrix[$i][$j]{pointer}变量等于 "none"它叫 last但它有什么作用呢?

另外,为什么 $matrix 变量使用花括号包含 score 和 pointer ? {score} ,我将其视为数组中的第 3 维,但这是其他内容吗?在谷歌上找不到任何关于此的信息。谢谢!

my @matrix;
$matrix[0][0]{score} = 0;
$matrix[0][0]{pointer} = "none";
#populate $matrix with more stuff

while (1) {
last if $matrix[$i][$j]{pointer} eq "none"; #<-what is this "last" doing?
#do some more stuff here
}

最佳答案

last 的可用用途命令:

  • 最后一个标签
  • 最后EXPR
  • 最后

  • 根据 Perl 的 documentation :

    The last command is like the break statement in C (as used in loops); it immediately exits the loop in question. If the LABEL is omitted, the command refers to the innermost enclosing loop. The last EXPR form, available starting in Perl 5.18.0, allows a label name to be computed at run time, and is otherwise identical to last LABEL . The continue block, if any, is not executed:


     LINE: while (<STDIN>) {
    last LINE if /^$/; # exit when done with header
    #...
    }

    last cannot be used to exit a block that returns a value such as eval {} , sub {} , or do {} , and should not be used to exit a grep or map operation. Note that a block by itself is semantically identical to a loop that executes once. Thus last can be used to effect an early exit out of such a block.

    关于perl - "last"在 Perl 中做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38170411/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com