in reply to Filtering files with lists of substitution patterns
BrowserUk’s solution is cleaner; but, in the spirit of TMTOWTDI, here’s an approach using the qr// operator:
#! perl use strict; use warnings; my @all_patterns = ( [ qr{#.*}, '' ], # 0. Comments [ qr{^\s+}, '' ], # 1. Initial whitespace [ qr{\s+$}, '' ], # 2. Final whitespace [ qr{^Total}, 'Sum' ], # 3. Initial "Total" --> "Sum" [ qr{^,}, '' ], # 4. Initial comma ); while (<DATA>) { my $string = apply_patterns($_, 0, 2, 3, 4); print "$string\n" if $string; } sub apply_patterns { my ($string, @indices) = @_; chomp $string; $string =~ s{$all_patterns[$_]->[0]} {$all_patterns[$_]->[1]}g for @indices; return $string; } __DATA__ First line # comment Total: Second line # Third line ,Fourth line
Output:
23:23 >perl 701_SoPW.pl First line Sum: Second line Fourth line 23:23 >
Note: Depending on the nature of the filters, you may need to give careful attention to the order in which they are applied.
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
|---|