Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

problems with tr///;

by Anonymous Monk
on Sep 25, 2001 at 22:25 UTC ( #114611=perlquestion: print w/replies, xml ) Need Help??

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

im trying to take $string and pull every \ out of it.

so i use:
$script =~ tr/\\//;
and it doesnt do anything. but if i try:
$script =~ tr/\\/X/;
it will change all \ into X
Any ideas as to why i cant get this to run properly?

Justin

Replies are listed 'Best First'.
Re: problems with tr///;
by suaveant (Parson) on Sep 25, 2001 at 22:31 UTC
    I think you want
    $script =~ tr/\\//d;

                    - Ant
                    - Some of my best work - Fish Dinner

      I think you're right. It's odd, but none of the books or docs that I have show any flags to run tr/// with... Thanks for your help on an insanely moronic question <you guys here are always nice, even when dumb questions are asked> Thanks again!
      Justin
        Check out perldoc perlop for the options to tr. As it appears our site docs are still a bit old, here is an excerpt from a Perl5.6.0 dist:
        c   Complement the SEARCHLIST.
        d   Delete found but unreplaced characters.
        s   Squash duplicate replaced characters.
        U   Translate to/from UTF-8.
        C   Translate to/from 8-bit char (octet). 
        

        jeffa

        none of the books or docs that I have show any flags to run tr/// with

        perldoc perlop
        It's odd, but none of the books or docs that I have show any flags to run tr/// with
        Please list the names of those books, so we can be sure to dis-recommend them for others.

        -- Randal L. Schwartz, Perl hacker

        My copy of the Camel certainly mentions all the tr///; switches. But, I'll admit when I first started Perl I would buy these books off the Street Vendors in NYC for $5. I got the Dummies book and one about Perl for Windows NT Servers. Both of them were barely worth even the little cash I shelled out, and left out major details. The O'Reilly books are popular for a reason.
Re: problems with tr///;
by broquaint (Abbot) on Sep 25, 2001 at 22:38 UTC
    The reason it won't work is because by default tr, transliterates, or changes, character for character between the search list to the replacement list. However if you stick a 'd' on the end it will delete everything in the search list that doesn't have a corresponding replacement e.g
    $var =~ tr/\\//d; # \ deleted, nothing to replace $var =~ tr/\\/X/d; # changed \ into X
    As someone is bound to say RTFM (man perlop).
    HTH

    broquaint

Re: problems with tr///;
by thunders (Priest) on Sep 25, 2001 at 23:37 UTC
    I go for substitution in cases like this in which case
    <code size=+1>$script =~ s/\\//g;</code>
    should do what your looking for.
    I often use tr to count stuff. as in
    <code size=+1>$x = $somestring =~ tr/0-9/0-9/;
    #find the number of digits in $somestring and put that value in $x</code>
Re: problems with tr///;
by Sigmund (Pilgrim) on Sep 26, 2001 at 13:14 UTC
    why use the translate operator?
    couldn't you use the substitution operator, like:

    $script =~ s/\\//g;

    (the g meaning "globally", as to say "match each \ in string".
    otherwise you could even use the numeric ascii code to specify a "\".

    SiG

    perl -le 's ssSss.s sSsSiss.s s$sSss.s s.$s\107ss.print'
      Because the transliteration operator is more efficient... I don't know why, but I know that it is.

                      - Ant
                      - Some of my best work - Fish Dinner

        <voice type="marvin_the_martian"<Oohh, you have made me very skeptical</voice>

        So I tried a couple of benchmarks. (I'm using 5.6.1, build 626 from ActiveState.) Here's the code:

        use Benchmark; $var = '\a\\aa\\\aaa\\\\aaaa'x100; print "s: "; timethis(100000, '$var =~ s/\\\//g'); # 3 \ for benchmark print "tr: "; timethis(100000, '$var =~ tr/\\\//d');
        and here's the results
        s: timethis 100000: 1 wallclock secs ( 0.51 usr + 0.00 sys = 0.51 +CPU) @ 195694.72/s (n=100000) tr: timethis 100000: 2 wallclock secs ( 1.68 usr + 0.00 sys = 1.68 +CPU) @ 59453.03/s (n=100000)
        You can play with $var and see the effect on the timings. Maybe tr used to be faster, but that's no longer the case.

        <soapbox>
        It's really beside the point. The point is the right tool for the job. In this case the right tool is the substitution operator. If you wanted to make multiple single character changes in the string then tr is what you want. Note the difference between:

        $var =~ tr /ab/cd/; # and $var =~ s /ab/cd/g;
        </soapbox>

        Have fun,
        Carl Forde

Re: problems with tr///;
by Anonymous Monk on Sep 25, 2001 at 23:09 UTC
    try s/\\//g; or better s#\##g; OEzzi
      s#\##g doesn't work. The substitution is incomplete. Tack another # on the end and see what happens:
      $_ = '\\##hello world'; s#\##g#; print;
      You still need to escape the "\" regardless of what seperator you use. The # seperator is useful for "/"

      Simon Flack ($code or die)
      $,=reverse'"ro_';s,$,\$,;s,$,lc ref sub{},e;$,
      =~y'_"' ';eval"die";print $_,lc substr$@,0,3;
Re: problems with tr///;
by DaveMWithnall (Initiate) on Sep 27, 2001 at 14:29 UTC
    you could try :
    $string =~ s/\\//g;
    which will do it.
      or : tr/\\/\0/;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2022-06-28 21:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (92 votes). Check out past polls.

    Notices?