in reply to Faster search and replace?

If you never change the expression (the repl_ref hash) you can start by adding the o modifier to the regexp:

s/($repl_str)/$$repl_ref{$1}/go;

That's the easiest and you might want to stop there if you're happy with the result.

If you want to speed it up more then you will have to work on the left part of the pattern. Is there any way you can match the repl_str stuff without a huge |? A character class maybe if you are looking for characters outside of the 0-127 range, or maybe an escape character and then an odd character? A mixture of the two? It depends on your data.

Replies are listed 'Best First'.
RE: Re: Faster search and replace?
by snax (Hermit) on Nov 09, 2000 at 15:12 UTC
    Oh -- right. Compile the regex once, right? That's a good idea. Further, you really point right to the problem -- I'm thinking about this "wrong" and just re-using code in a way that isn't efficient.

    My snippet is great for little form letter types of things where I don't know too much about the problem beforehand, but in this case I know very precisely beforehand the characters that are "wrong" and for each one I know the "right" replacement -- so I should be using a

    eval("tr/$wronglist/$rightlist/");
    which has got to be better optimized for this kind of application.

    Interesting: someone else has done this same thing and found (apparently) a memory leak. I guess I'm on the right track now.

    Thanks for the new perspective! As always, TMTOWTDI, and some are better than others :)