Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2024-04-20 03:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found