Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Need to reset a variable to zero ??

by dkhalfe (Acolyte)
on Aug 06, 2012 at 17:26 UTC ( #985776=perlquestion: print w/ replies, xml ) Need Help??
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.

Comment on Need to reset a variable to zero ??
Select or Download Code
Replies are listed 'Best First'.
Re: Need to reset a variable to zero ??
by choroba (Canon) 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://985776]
Approved by lidden
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2015-07-31 03:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (274 votes), past polls