in reply to Re^3: Doing "it" only once
in thread Doing "it" only once
To answer you code question - consider the following trivial example:
To re-write this we need to first changefor ( @some_array ) { if ( $_ eq 'foo' ) { print "skipping foo\n"; next; } if ( $_ eq 'bar' ) { handle_bar($_); next; } handle_rest($_); }
So that when we break out of the first loop to enter the second, we can remember where we left off. Unfortunately, the order that we will encounter 'foo' and 'bar' is unknown so we also have to create a flag variable and end up with 4 while loops instead of the original 1:for ( @some_array ) { ... } # to my $index = -1; while ( ++$index <= $#some_array ) { ... }
my $flag; my $index = -1; while ( ++$index <= $#some_array ) { if ( $_ eq 'foo' ) { $flag = 'foo'; print "skipping foo\n"; last; } if ( $_ eq 'bar' ) { $flag = 'bar'; handle_bar($_); last; } handle_rest($_); } if ( $flag eq 'foo' ) { while ( ++$index <= $#some_array ) { if ( $_ eq 'bar' ) { handle_bar($_); last; } handle_rest($_); } } else { while ( ++$index <= $#some_array ) { if ( $_ eq 'foo' ) { print "skipping foo\n"; last; } handle_rest($_); } } while ( ++$index <= $#some_array ) { handle_rest($_); }
Cheers - L~R
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Doing "it" only once
by Roy Johnson (Monsignor) on Sep 21, 2005 at 17:24 UTC | |
by Limbic~Region (Chancellor) on Sep 21, 2005 at 17:37 UTC | |
by Roy Johnson (Monsignor) on Sep 21, 2005 at 18:35 UTC | |
|
Re^5: Doing "it" only once
by Anonymous Monk on Sep 22, 2005 at 08:22 UTC | |
by Limbic~Region (Chancellor) on Sep 22, 2005 at 12:23 UTC |