in reply to simple letter substitution according to hash

IIUC

s/([[:alpha:]])/$hash{$1}||$1/ge;

I would also avoid capturing:

s/[[:alpha:]]/$hash{$&}||$&/ge;

but so many people dislike it, so I gave the one with capturing first. Of course tr is generally better suited for this kind of task, as explained in in perldoc perlop, but unfortunately the transliteration table is built at compile time, so you can not ionterpolate data into it. But you can adopt the eval workaround also described in perldoc perlop.

Replies are listed 'Best First'.
Re^2: simple letter substitution according to hash
by ikegami (Patriarch) on May 16, 2006 at 15:15 UTC

    What do you mean, avoid capturing? Using $& causes all regexps to become capturing regexps. You're not saving anything by using $&, and you end up slowing all non-capturing regexps in that interpreter instance.

      I meant "capturing parentheses". Or "explicit capturing". Sorry for the imprecision.

      You're not saving anything by using $&, and you end up slowing all non-capturing regexps in that interpreter instance.

      <rant with="perl" not="you">
      You're right, of course. It's just so annoying to have this technical inconvenience. If I match something it is quite probable that I may want to do something with it. Hence the need for a pronoun without the need to say "hey, capture it all for me". Different story for $` and $', obviously.
      </rant>