in reply to Re: A way to avoid repeated conditional loops
in thread A way to avoid repeated conditional loops
You can't rely on readline returning EOF or errors more than once. Fix:
OUTER: { for (;;) { defined( my $_ = <$fh> ) or last OUTER; if (/alpha/) { f(); last; } else { g(); } } while (<$fh>) { g(); } }
Update: The remainder of this post is wrong.
You can get rid of the duplicate g() by making the loops bottom tested.
OUTER: { defined( my $_ = <$fh> ) or last OUTER; for (;;) { if (/alpha/) { f(); last; } defined( $_ = <$fh> ) or last OUTER; } for (;;) { g(); defined( $_ = <$fh> ) or last OUTER; } }
You can squish it down a bit:
LOOP: { defined( my $_ = <$fh> ) or last; if (/alpha/) { f(); redo; } for (;;) { g(); defined( $_ = <$fh> ) or last; } }
Look at that, g() is not even duplicated anymore!!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: A way to avoid repeated conditional loops
by jffry (Hermit) on Aug 24, 2011 at 19:39 UTC | |
by ikegami (Patriarch) on Aug 24, 2011 at 20:36 UTC | |
by jffry (Hermit) on Aug 24, 2011 at 21:46 UTC | |
|
Re^3: A way to avoid repeated conditional loops
by Deus Ex (Scribe) on Aug 25, 2011 at 13:41 UTC | |
by ikegami (Patriarch) on Aug 25, 2011 at 17:45 UTC | |
by Deus Ex (Scribe) on Aug 29, 2011 at 03:44 UTC |