gpt4 book ai didi

perl - 是否可以在一行中编写 Perl foreach 循环?

转载 作者:行者123 更新时间:2023-12-03 21:10:47 25 4
gpt4 key购买 nike

是否可以使用单行 foreach在 Perl 中循环?

$hash{$thing}++ foreach my $thing (@things);

我知道这可以通过许多其他命令实现,例如,
die "Invalid file!\n" if (open($Handle, "file.txt"));

我知道公开声明可能已损坏:)

最佳答案

几乎。在foreach后缀中,必须使用$_:

$hash{$_}++ foreach @things;

或者等效地(因为 forforeach 是语法的别名):
$hash{$_}++ for @things;

关于perl - 是否可以在一行中编写 Perl foreach 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7080783/

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