in reply to Re^2: Remove blank lines from REGEX output
in thread Remove blank lines from REGEX output
This is much cleaner and much more readable than a long series of nested if ... elsif ... elsif ... It is also often quite efficient, because as soon as you discard a line for one reason, none of the subsequent tests has to run (of course, it will be more efficient if you are able to put first the most common causes for exclusions and last the rare ones). There are other ways of achieving similar results. For example, you could have:while (<$IN>) { chomp; next if /^#/; # discard line, it is a comment (starts + with #) next if /^\s*$/; # discard line, contains only spaces next if length < $min_length; # line is too short next if /^REM/; # another form of comment next unless /^.{3}\d{4}/; # lines of interest have 4 digits from +position 4 to 7 # now the real processing ... }
This is more concise, and any condition evaluating to TRUE will also lead to short-circuiting the subsequent conditions, so that the performance will be similar, but that removes the opportunity to document the business rules that led to exclusion. I might use any of the two techniques, depending on the situation, but if the business rules are somewhat complicated or numerous, I prefer the first one.while (<$IN>) { chomp; next if /^#/ or /^\s*$/ or length < $min_useful_length or /^REM/ + or not /^.{3}\d{4}/ ...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Remove blank lines from REGEX output
by AnomalousMonk (Archbishop) on Feb 06, 2014 at 00:22 UTC | |
by Laurent_R (Canon) on Feb 06, 2014 at 11:02 UTC | |
by AnomalousMonk (Archbishop) on Feb 06, 2014 at 11:51 UTC | |
by Laurent_R (Canon) on Feb 06, 2014 at 14:22 UTC |