Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Don't print on the last loop

by jdhedden (Deacon)
on Jun 13, 2005 at 14:13 UTC ( [id://466169]=note: print w/replies, xml ) Need Help??


in reply to Don't print on the last loop

The easiest way is to move the comma printing to the front of the loop, and add a flag to control printing it:
my $add_comma=0; while ( my ( $cat, $schem, $tn, $cn, $dt, $tyn, $cs, $bl, $dd, $npr +, $nul) = $sth->fetchrow_array()) { if ($add_comma++) { print ",\n"; } print "\t$cn "; if (($tyn eq 'DATETIME') || ($tyn eq 'DOUBLE')) { print "$tyn"; } else { print "$tyn($cs)"; } print " not null" if ($nul == 0); }

Remember: There's always one more bug.

Replies are listed 'Best First'.
Re^2: Don't print on the last loop
by calin (Deacon) on Jun 13, 2005 at 20:20 UTC

    Another idea is to unroll the first iteration of the loop. This way you don't have to test for a flag on subsequent iterations. I believe this is the correct approach optimisation-wise - if one condition is true only once and then guaranteed false for the next 1.000.000 times, checking it programatically (and doing other stuff like modifying the flag variable) 1.000.001 times is a waste. Pseudocode:

    if ( CONDITION ) { do_iteration(); while ( CONDITION ) { print ",\n"; do_iteration(); } }
Re^2: Don't print on the last loop
by omega_monk (Scribe) on Jun 13, 2005 at 14:30 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://466169]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2025-05-24 13:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.