Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: transliteration d flag not working as expected

by AnomalousMonk (Abbot)
on Jun 09, 2013 at 12:04 UTC ( #1037924=note: print w/ replies, xml ) Need Help??


in reply to Re: transliteration d flag not working as expected
in thread transliteration d flag not working as expected

You could also do it with a single  s/// substitution, although it is messier and almost certainly slower than a double-tr/// approach:

>perl -wMstrict -le "my %xlate = qw(t f h l i i s p); ;; my $s = 'alters'; $s =~ s{ . }{ $xlate{${^MATCH}} // '' }xmsgpe; print qq{'$s'}; " 'fp'


Comment on Re^2: transliteration d flag not working as expected
Select or Download Code
Re^3: transliteration d flag not working as expected
by LanX (Canon) on Jun 09, 2013 at 12:29 UTC
    Not sure if it's slower, 'tr///' doesn't allow variable interpolation.

    Using it twice in a DRY way means putting the pattern into a variable which leads to eval :(

    Because the transliteration table is built at compile time, ne +ither the SEARCHLIST nor the REPLACEMENTLIST are subjected to dou +ble quote interpolation. That means that if you want to use va +riables, you must use an eval():

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      What I had in mind was that  s/// would be messier/slower if interpolation was unnecessary, as the OPed problem statement seemed to indicate. I agree that if interpolation becomes necessary,  s/// is almost certainly preferable to an eval approach.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (11)
As of 2015-07-06 11:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (72 votes), past polls