小编Sal*_*din的帖子

perl'last'语句和变量范围

应该是简单的Perl构造不像我期望的那样.

@closest数组包含"NNN-NNN"形式的字符串.我试图找到第一个数组元素,其中初始"NNN"匹配特定值.

由于我已经$compoundKey在循环外声明,我希望它是持久的.循环行为符合预期,当我得到匹配时退出.但是,在代码退出循环后,$compoundKey变量为空.(请参阅代码后的日志输出.我正在寻找"83".匹配的元素不是数组中的最后一个元素.)

    my $compoundKey;
    foreach $compoundKey (@closest)
    {
        logentry("In loop compoundKey is $compoundKey\n");
        last if ($compoundKey =~ /^$RID/);   
    }
    logentry("Outside loop compoundKey is $compoundKey\n");
Run Code Online (Sandbox Code Playgroud)

日志文件:

    2019-02-27 18:15:26: In loop compoundKey is 90-1287
    2019-02-27 18:15:26: In loop compoundKey is 86-1223
    2019-02-27 18:15:26: In loop compoundKey is 86-1235
    2019-02-27 18:15:26: In loop compoundKey is 87-1316
    2019-02-27 18:15:26: In loop compoundKey is 89-1219
    2019-02-27 18:15:26: In loop compoundKey is 83-1224
    2019-02-27 18:15:26: Outside loop compoundKey is …
Run Code Online (Sandbox Code Playgroud)

perl loops break

3
推荐指数
1
解决办法
70
查看次数

标签 统计

break ×1

loops ×1

perl ×1