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,


In reply to Re: Filtering files with lists of substitution patterns by Athanasius
in thread Filtering files with lists of substitution patterns by LinuxMatt

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.