Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

How Do I Use the tr Function's 'c' Option?

by anniyan (Monk)
on Aug 24, 2005 at 16:26 UTC ( #486270=perlquestion: print w/ replies, xml ) Need Help??
anniyan has asked for the wisdom of the Perl Monks concerning the following question:

Monks, I have one question realated to translation.

I have read the following from an online material.

The c option (c is for "complement") translates all characters that are not specified. For example, the statement $string =~ tr/\d/ /c; replaces everything that is not a digit with a space.

But when i tried the following i am getting the following output than what i expected.

$a = 'acvdef431234'; $a =~ tr/\d/ /c; print "$a"; output: d expected output: 431234

But if i used [0-9] it is working good.

$a = 'acvdef431234'; $a =~ tr/[0-9]/ /c; print "$a"; output: 431234

I am in total confusion. Could anyone explain whats going on there.

Regards,
Anniyan
(CREATED in HELL by DEVIL to s|EVILS|GOODS|g in WORLD)

2005-08-24 Retitled by Arunbear, as per Monastery guidelines
Original title: 'tr///c'

Comment on How Do I Use the tr Function's 'c' Option?
Select or Download Code
Re: How Do I Use the tr Function's 'c' Option?
by monkey_boy (Curate) on Aug 24, 2005 at 16:30 UTC
    Straight from perlop
    Note that tr does not do regular expression character classes such as +\d or [:lower:].





    This is not a Signature...
Re: How Do I Use the tr Function's 'c' Option?
by halley (Prior) on Aug 24, 2005 at 16:34 UTC
    When in doubt, trust the actual Perl documentation over any other website. Type "perldoc perlop" at your command line.

    The issue with the unofficial documentation you found is that tr does not take special regular expression escapes like "\d" or bracketed classes like "[abc]" in its arguments.

      Note that "tr" does not do regular expression character classes such as "\d" or "[:lower:]".
    $string =~ tr/0123456789/ /c;
    It does support ranges, but use no brackets. Shortcut the above with the special hyphen:
    $string =~ tr/0-9/ /c;

    --
    [ e d @ h a l l e y . c c ]

Re: How Do I Use the tr Function's 'c' Option?
by japhy (Canon) on Aug 24, 2005 at 18:58 UTC

      Nope ... quick google leads me to believe it's Perl Unleashed.

      I wonder if I could convince my congressman to introduce a "expired now because it's crappy and wrong" copyright clause.

      -derby
        Since he italicized his source text, I did a google search with the exact text "replaces everything that is not a digit with a space" which yielded my three results.

        Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
        How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

      japhy you are right, i read in perl in 21 days. So the documentation is wrong.oops.

      Regards,
      Anniyan
      (CREATED in HELL by DEVIL to s|EVILS|GOODS|g in WORLD)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2014-08-21 20:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (143 votes), past polls