lsxo has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks!! I have a for loop and I would like to have it sleep for 5 seconds every four iterations of the loop. I know this will include a counter and a mod however I am have dificulty with the syntax to do so. The loop will iterate about 600 times. Thanks!

Replies are listed 'Best First'.
Re: help with sleep()
by moritz (Cardinal) on Aug 11, 2009 at 13:37 UTC
    Show us what you have tried so far, and where your problems are.

    Modulo is spelled $variable % 4 in Perl, see perlop.

      nevermind I figured it out. Sorry!

        There's no need to be sorry, just remember to ask your questions with detail and show some code if possible. On the other hand, sharing your solution may help other fellow monks and yourself too if there's room for improvement.

        Sharing is caring :)

Re: help with sleep()
by kennethk (Abbot) on Aug 11, 2009 at 13:47 UTC
    If you read How do I post a question effectively?, you'll see a strong emphasis on "show us what you've done". Part of it is because we don't like just doing people's work for them and part is so we have a better idea of what your difficulties might be.

    You seem to have a rough idea for your algorithm, so I'll translate what you've written into code - remember, of course, that there's more than one way to do it. See sleep and perlop for details on code elements.

    foreach my $i (0 .. 600) { print $i; if ($i % 4 == 3) { sleep(5); } }
Re: help with sleep()
by Bloodnok (Vicar) on Aug 11, 2009 at 13:41 UTC
    You don't show what you've tried ... neither have you elaborated on the problems you encountered on the way - see How do I post a question effectively?.

    I've always found/noted that, here in the Monastry, the chances of getting more help increase in proportion with the volume of information provided.

    A user level that continues to overstate my experience :-))
Re: help with sleep()
by JavaFan (Canon) on Aug 11, 2009 at 13:42 UTC
    I know this will include a counter and a mod however
    I can do it without a mod. I can even do it without a counter if I wanted to.