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

transliteration d flag not working as expected

by SamCG (Hermit)
on Jun 09, 2013 at 11:03 UTC ( [id://1037919]=perlquestion: print w/replies, xml ) Need Help??

SamCG has asked for the wisdom of the Perl Monks concerning the following question:

Hi as a sample:
C:\>perl -e "$_=alter;tr/this/flip/d;print;" alfer
Shouldn't this be producing just "f", as the d flag should cause unmatched items to be deleted?



-----------------
s''limp';@p=split '!','n!h!p!';s,m,s,;$s=y;$c=slice @p1;so brutally;d;$n=reverse;$c=$s**$#p;print(''.$c^chop($n))while($c/=$#p)>=1;

Replies are listed 'Best First'.
Re: transliteration d flag not working as expected
by BrowserUk (Patriarch) on Jun 09, 2013 at 11:20 UTC
    as the d flag should cause unmatched items to be deleted?

    No. The /d flag is specified as: "Delete found but unreplaced characters.". Ie. Any character unmatched by the searchlist is left untouched

    Since your search and replace lists are equal length, any characters matched will always be replaced, so nothing will ever be deleted.

    Deletions will only occur if the replacement list is shorter than the search list; including the frequently used case of a null replacement list.

    In addition, if you also specify /c, then only the t (ie. pre-translated f) will remain:

    $s = 'alter'; $s =~ tr[this][flip]cd; print $s;; t

    So, to get the effect you expected you could do two passes:

    $s = 'alter'; $s =~ tr[this][flip]cd; print $s; t $s =~ tr[this][flip]; print $s;; f

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: transliteration d flag not working as expected
by hdb (Monsignor) on Jun 09, 2013 at 11:24 UTC

    From "Quote-and-Quote-like-Operators":
    If the /d modifier is specified, any characters specified by SEARCHLIST not found in REPLACEMENTLIST are deleted.

    This means if you do not specify a replacement for some character, it will get deleted, but only if it is matched.

Re: transliteration d flag not working as expected
by LanX (Saint) on Jun 09, 2013 at 11:41 UTC
    you can achieve the desired result by combining two tr

    DB<112> $_="alter" => "alter" DB<113> tr/this//cd # delete anything except t,h,i,s => 4 DB<114> $_ => "t" DB<115> tr/this/flip/ # translate => 1 DB<116> $_ => "f"

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      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'
        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)

Log In?
Username:
Password:

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

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

    No recent polls found