if you have a lot of conditions, then perhaps something like this is appropriate:#!usr/bin/perl -w use strict; while (<DATA>) { if(/\balpha\b/) { print "term 'alpha' detected on input line $.\n"; next; } .. do something here... .. sometimes better than elsif() } __DATA__ beta beta beta alpha # this line appears only once in the whole file beta beta beta
One of the reasons to do it that way is so that all of the conditions can be interchanged with one another. If you have an if(..)elsif(..)else(..) conditional, this hinders the ability to change the order of the conditions. At the end of the day, that approach will result in more complex code and limit the ability to re-order the code's priority of matching - don't do it. At least for number of "if" statements of about 5-6.while ( <FILE> ) { if ( /alpha/ ) { .... next; } if (/beta/) { .... next; } #here is the default code... }
Update: Well this is one of those situations where there just isn't a "one size fits all" solution. I tend to prefer coding with several sequential if{..} statements so that all of them are "equal". This whole coding style discussion can set off something that will last until a gallon of gas falls back to $1.60.
In reply to Re: A way to avoid repeated conditional loops
by Marshall
in thread A way to avoid repeated conditional loops
by Deus Ex
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |