I'm trying to write a small Perl script which needs to add sleep time in order to delay the execution commands in the for loop.
here is the example.
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);
}
The output is very simple too
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
But I have to increase the sleep time for every 5th iteration example should be like below
First 5 lines should be delayed with out sleep time like this
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
The next 5 lines should be delayed with 5 seconds sleet time like this
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
And then every 5 iterations the sleep time should increase with 5 seconds.
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
Can someone please advise on how to get it working that way?
CodePudding user response:
Then change the delay every 5-th iteration, using the modulo (%) operator for example, just as needed
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
}
CodePudding user response:
Use nested for
loops:
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;
}
Output:
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.
CodePudding user response:
for my $i ( 1.. $num ) {
my $d = int( ( $i - 1 ) / 5 ) * 5;
say "delaying iteration $i by $d";
sleep($d);
}
or
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]
...
CodePudding user response:
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
...