3

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?

4 Answers 4

2

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
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1
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]
...

4 Comments

thanks for the reply too, I just noticed that the first five executions does have same time but the next ones keep adding the +5 on each execution. ``` my $n = 50; my $d = 0; for my $i ( 1.. $n ) { say "delaying iteration $i by $d"; print strftime "%T", localtime time; sleep($d); $d += 5 if $i % 5 == 0; } ````
this is the output with time added delaying iteration 1 by 0 22:30:48delaying iteration 2 by 0 22:30:48delaying iteration 3 by 0 22:30:48delaying iteration 4 by 0 22:30:48delaying iteration 5 by 0 22:30:48delaying iteration 6 by 5 22:30:48delaying iteration 7 by 5 22:30:53delaying iteration 8 by 5 22:30:58delaying iteration 9 by 5 22:31:03delaying iteration 10 by 5
so the first five should have started at same time then the next five should start on same time with sleep time.
That's completely unreadable. If there's a problem with the question edit the question and leave a comment letting me know.
-1

You have indicated in the comments that the delays you mentioned are relative to the start of the loop. In that case, you want the following:

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
...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.