Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
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
Re: Need to reset a variable to zero ??
by choroba (Abbot) 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 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.

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; } }

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 having an uproarious good time at the Monastery: (18)
As of 2014-10-30 17:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (208 votes), past polls