Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Substitution problem.

by snax (Hermit)
on Nov 25, 2000 at 20:13 UTC ( [id://43314]=note: print w/replies, xml ) Need Help??


in reply to Substitution problem.

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://43314]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (11)
As of 2024-04-18 14:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found