Why don't you try to do this more "all at once"?

First, note that tr/// is the right tool for single character substitution lists -- it's heavily optimized for this kind of thing.

Second, it seems to me that your $count variable may be redundant. Why not make one hash with keys the "old" values and the values the "new" values -- that sounds odd, but basically:

$replacements{quotemeta($array1{$count})} = $array2{$count};
...but do this where you are building %array1 and %array2 so you don't need to use $count or the array hashes at all. Aside: in perl's common parlance, "array" typically refers to an integer indexed object like you are using your hashes above, but the hash construct is much more flexible (and more expensive to use in terms of resources).

Anyway, with a %replacements hash you can then do:

my $repl_str = join( '|', keys %replacements); $someText =~ s/($replstr)/%replacements{$1}/g;
This does all your replacements with one evaluation.

Again, note that this is really only appropriate when you need to replace more than single characters.


In reply to Re: Substitution problem. by snax
in thread Substitution problem. by Leitchn

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.