[Denver-pm] short circuiting a loop from a sub?

Stuart A Johnston saj at thecommune.net
Fri Apr 4 13:34:13 PDT 2014


   if ( $Loop =~ /Foo/ ) { &CheckTwo ; next }

You are essentially doing a switch statement. There are a few other 
styles to consider:

http://perldoc.perl.org/perlfaq7.html#How-do-I-create-a-switch-or-case-statement%3F

On 04/04/2014 02:27 PM, Robert L. Harris wrote:
>
> Given this (go for the process flow, not the actual code ) :
>
> $Var[0]="Foo";
> $Var[1]="Bar";
> $Var[2]="Baz";
>
> foreach $Loop ( @Var ) {
>    &CheckOne if ( $Loop =~ /Test1/ );
>    &CheckTwo if ( $Loop =~ /Foo/ );
>    &CheckThree if ( $Loop =~ /Test3/ );
>    &CheckFour if ( $Loop =~ /Test4/ );
>    &CheckFive if ( $Loop =~ /Test5/ );
>    &CheckSix if ( $Loop =~ /Test6/ );
> }
>
> sub CheckTwo {
>    # Do some work
> }
>
>
>    There are currently about 15 "Check" sub routines with unique matches
> (One $Var will no match multiple sub-procs.  Is there a way so that if I
> match on CheckTwo, it will do the equivalent of "next" and go to the
> next iteration of the loop instead of walking through the rest of the tests?
>
>    It doesn't seem like much but I'm parsing almost a gig of data
> regularly and every check I can skip, the better.
>
> Robert
>
>
> --
> :wq!
> ---------------------------------------------------------------------------
> Robert L. Harris
>
> DISCLAIMER:
>        These are MY OPINIONS             With Dreams To Be A King,
>         ALONE.  I speak for                      First One Should Be A Man
>         no-one else.                                     - Manowar
>
>
> _______________________________________________
> Denver-pm mailing list
> Denver-pm at pm.org
> http://mail.pm.org/mailman/listinfo/denver-pm
>



More information about the Denver-pm mailing list