If your substitutions are all literal substitutions, i.e., all of the form
    'banana' => 'plum'
    'xyzzy'  => 'whatever'
and never of the form
    /f[eio]e+/ => 'something'
and definitely not the sort of thing BillKSmith is doing here, then the following approach might be helpful. If you have many substitutions to make, the alternation of the  $search regex may grow quite large, but with the alternation trie optimization of Perl version 5.10, this should not be a problem — unless it gets really big! (My guess is that 10K search-replace pairs could be handled.)

c:\@Work\Perl>perl -le "use 5.010; ;; use warnings; use strict; ;; my %direct_substitution = ( 'apple' => 'PEAR', 'red' => 'YELLOW', 'xyzzy' => 'SOME OTHER THING', ); ;; my ($search) = map qr{ \b (?: $_ ) \b }xms, join q{ | }, keys %direct_substitution ; print $search; ;; my @lines = ( 'apple xapple apple applex xapplex apple', 'red', 'xxx red apple yyyy xyzzy zz', ); ;; my $count = 0; $count += s{ ($search) }{$direct_substitution{$1}}xmsg for @lines; ;; print qq{substitutions: $count}; print qq{'$_'} for @lines; " (?^msx: \b (?: apple | red | xyzzy ) \b ) substitutions: 7 'PEAR xapple PEAR applex xapplex PEAR' 'YELLOW' 'xxx YELLOW PEAR yyyy SOME OTHER THING zz'
Of course, you would go through your  @substitutionlist array to build the  %direct_substitution hash before processing the  @lines array.


Give a man a fish:  <%-{-{-{-<


In reply to Re: How to count substitutions on an array by AnomalousMonk
in thread How to count substitutions on an array by Anonymous Monk

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.