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

Hello all. Here is my code

use strict; my $z = 0; for (my $d=1; $d < 10; $d++) { $z++; if ($z==1) { print $headers, "\n"; } else { print $body, "\n"; } }

Note:  $headers and  $body are defined elsewhere in my code and do not necessarily pertain to my question

Question: Is there a way to reset the  $z variable to zero each time the for loop iterates? I am trying to print the header each time the loop iterates.

Replies are listed 'Best First'.
Re: Need to reset a variable to zero ??
by choroba (Cardinal) on Aug 06, 2012 at 17:32 UTC
    If you want to print the header each time the loop iterates, just remove $z and the if condition entirely.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Need to reset a variable to zero ??
by rpnoble419 (Pilgrim) on Aug 06, 2012 at 21:40 UTC
    or try:
    use strict; my $z = 1; for (my $d=1; $d < 10; $d++) { if ($z==1) { print $headers, "\n"; $z=0; } else { print $body, "\n"; $z=1; } }
Re: Need to reset a variable to zero ??
by abualiga (Scribe) on Aug 06, 2012 at 18:13 UTC

    Agree with choroba. If all you need to do is print a header n-times, why not simply iterate over 'for' loop n-times? Unless there is more to your question.