SPUG: Loop styles: goto vs. redo

Yitzchak Scott-Thoennes sthoenna at efn.org
Wed Jul 3 16:46:22 CDT 2002


In place of:

{
    my $done = 1;
    for (list) {
        unless(condition) {
            body;
            $done = 0;
        }
    }
    redo unless $done;
}

you could say:

1 while grep {
  if (!condition) {  # can't use unless
     body;
     1; # needed only if last stmt of body is false or needs void context
  }
} list;

or more simply:

1 while grep !condition && do { body; 1 }, list;

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org




More information about the spug-list mailing list