Perl for循环批量迭代
Perl for loop iteration in batch
我正在尝试编写一个小的 Perl 脚本,它需要添加睡眠时间以延迟 for 循环中的执行命令。
这里是例子。
use strict;
use warnings;
use v5.26.1;
my $delay = 10;
my $num = 100;
for (my ($i, $d) = (1, $delay); $i <= $num; $i++, $d+=5 ) {
say "delaying iteration $i by $d";
sleep($d);
}
输出也很简单
delaying iteration 1 by 10
delaying iteration 2 by 15
delaying iteration 3 by 20
delaying iteration 4 by 25
delaying iteration 5 by 30
但是我必须增加每 5 次迭代的睡眠时间
示例应如下所示
前 5 行应该像这样延迟没有睡眠时间
delaying iteration 1 by 0
delaying iteration 2 by 0
delaying iteration 3 by 0
delaying iteration 4 by 0
delaying iteration 5 by 0
接下来的 5 行应该像这样延迟 5 秒的雨雪时间
delaying iteration 6 by 5
delaying iteration 7 by 5
delaying iteration 8 by 5
delaying iteration 9 by 5
delaying iteration 10 by 5
然后每 5 次迭代睡眠时间应增加 5 秒。
delaying iteration 11 by 10
delaying iteration 12 by 10
delaying iteration 13 by 10
delaying iteration 14 by 10
delaying iteration 15 by 10
有人可以建议如何让它以这种方式工作吗?
使用嵌套 for
循环:
use strict;
use warnings;
use feature 'say';
my $num = 7;
my $d = 0;
my $i = 0;
for my $j (1 .. $num) {
for (1 .. 5) {
$i++;
say "delaying iteration $i by $d";
sleep($d);
}
$d += 5;
}
输出:
delaying iteration 1 by 0
delaying iteration 2 by 0
delaying iteration 3 by 0
delaying iteration 4 by 0
delaying iteration 5 by 0
delaying iteration 6 by 5
delaying iteration 7 by 5
delaying iteration 8 by 5
delaying iteration 9 by 5
delaying iteration 10 by 5
delaying iteration 11 by 10
delaying iteration 12 by 10
delaying iteration 13 by 10
delaying iteration 14 by 10
delaying iteration 15 by 10
delaying iteration 16 by 15
delaying iteration 17 by 15
etc.
for my $i ( 1.. $num ) {
my $d = int( ( $i - 1 ) / 5 ) * 5;
say "delaying iteration $i by $d";
sleep($d);
}
或
my $d = 0;
for my $i ( 1.. $num ) {
say "delaying iteration $i by $d";
sleep($d);
$d += 5 if $i % 5 == 0;
}
delaying iteration 1 by 0
[0 second pause]
delaying iteration 2 by 0
[0 second pause]
delaying iteration 3 by 0
[0 second pause]
delaying iteration 4 by 0
[0 second pause]
delaying iteration 5 by 0
[0 second pause]
delaying iteration 6 by 5
[5 second pause]
delaying iteration 7 by 5
[5 second pause]
delaying iteration 8 by 5
[5 second pause]
delaying iteration 9 by 5
[5 second pause]
delaying iteration 10 by 5
[5 second pause]
delaying iteration 11 by 10
[10 second pause]
...
然后每 5 次迭代更改延迟,例如使用 modulo (%) operator,
随心所欲
use warnings;
use strict;
use feature 'say';
my $delay = 0;
my $num = 100;
foreach my $n (1 .. $num) {
say $n;
sleep $delay;
$delay += 5 if $n % 5 == 0; # every 5-th
}
您在评论中指出,您提到的延迟是相对于循环开始的。在这种情况下,您需要以下内容:
for my $i ( 1.. $num ) {
sleep( 5 ) if $i % 5 == 1 && $i != 1;
say "iteration $i";
}
iteration 1
iteration 2
iteration 3
iteration 4
iteration 5
[5 second pause]
iteration 6
iteration 7
iteration 8
iteration 9
iteration 10
[5 second pause]
iteration 11
...
我正在尝试编写一个小的 Perl 脚本,它需要添加睡眠时间以延迟 for 循环中的执行命令。
这里是例子。
use strict;
use warnings;
use v5.26.1;
my $delay = 10;
my $num = 100;
for (my ($i, $d) = (1, $delay); $i <= $num; $i++, $d+=5 ) {
say "delaying iteration $i by $d";
sleep($d);
}
输出也很简单
delaying iteration 1 by 10
delaying iteration 2 by 15
delaying iteration 3 by 20
delaying iteration 4 by 25
delaying iteration 5 by 30
但是我必须增加每 5 次迭代的睡眠时间 示例应如下所示
前 5 行应该像这样延迟没有睡眠时间
delaying iteration 1 by 0
delaying iteration 2 by 0
delaying iteration 3 by 0
delaying iteration 4 by 0
delaying iteration 5 by 0
接下来的 5 行应该像这样延迟 5 秒的雨雪时间
delaying iteration 6 by 5
delaying iteration 7 by 5
delaying iteration 8 by 5
delaying iteration 9 by 5
delaying iteration 10 by 5
然后每 5 次迭代睡眠时间应增加 5 秒。
delaying iteration 11 by 10
delaying iteration 12 by 10
delaying iteration 13 by 10
delaying iteration 14 by 10
delaying iteration 15 by 10
有人可以建议如何让它以这种方式工作吗?
使用嵌套 for
循环:
use strict;
use warnings;
use feature 'say';
my $num = 7;
my $d = 0;
my $i = 0;
for my $j (1 .. $num) {
for (1 .. 5) {
$i++;
say "delaying iteration $i by $d";
sleep($d);
}
$d += 5;
}
输出:
delaying iteration 1 by 0
delaying iteration 2 by 0
delaying iteration 3 by 0
delaying iteration 4 by 0
delaying iteration 5 by 0
delaying iteration 6 by 5
delaying iteration 7 by 5
delaying iteration 8 by 5
delaying iteration 9 by 5
delaying iteration 10 by 5
delaying iteration 11 by 10
delaying iteration 12 by 10
delaying iteration 13 by 10
delaying iteration 14 by 10
delaying iteration 15 by 10
delaying iteration 16 by 15
delaying iteration 17 by 15
etc.
for my $i ( 1.. $num ) {
my $d = int( ( $i - 1 ) / 5 ) * 5;
say "delaying iteration $i by $d";
sleep($d);
}
或
my $d = 0;
for my $i ( 1.. $num ) {
say "delaying iteration $i by $d";
sleep($d);
$d += 5 if $i % 5 == 0;
}
delaying iteration 1 by 0
[0 second pause]
delaying iteration 2 by 0
[0 second pause]
delaying iteration 3 by 0
[0 second pause]
delaying iteration 4 by 0
[0 second pause]
delaying iteration 5 by 0
[0 second pause]
delaying iteration 6 by 5
[5 second pause]
delaying iteration 7 by 5
[5 second pause]
delaying iteration 8 by 5
[5 second pause]
delaying iteration 9 by 5
[5 second pause]
delaying iteration 10 by 5
[5 second pause]
delaying iteration 11 by 10
[10 second pause]
...
然后每 5 次迭代更改延迟,例如使用 modulo (%) operator, 随心所欲
use warnings;
use strict;
use feature 'say';
my $delay = 0;
my $num = 100;
foreach my $n (1 .. $num) {
say $n;
sleep $delay;
$delay += 5 if $n % 5 == 0; # every 5-th
}
您在评论中指出,您提到的延迟是相对于循环开始的。在这种情况下,您需要以下内容:
for my $i ( 1.. $num ) {
sleep( 5 ) if $i % 5 == 1 && $i != 1;
say "iteration $i";
}
iteration 1
iteration 2
iteration 3
iteration 4
iteration 5
[5 second pause]
iteration 6
iteration 7
iteration 8
iteration 9
iteration 10
[5 second pause]
iteration 11
...