Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: Need a one liner

by crabbdean (Pilgrim)
on Jul 17, 2004 at 10:31 UTC ( #375221=note: print w/replies, xml ) Need Help??

in reply to Need a one liner

You want the translation operator:
$a =~ tr/\W_//g;
or you can also use the substitution operator but specifying a choice: UPDATE to CODE: (opps)
$a =~ s/(\W|_)//g;
UPDATE: It might also be worth your while to read the perlop documentation

The Funkster of Mirth
Programming these days takes more than a lone avenger with a compiler. - sam
RFC1149: A Standard for the Transmission of IP Datagrams on Avian Carriers

Replies are listed 'Best First'.
Re^2: Need a one liner
by davido (Archbishop) on Jul 17, 2004 at 15:30 UTC
    $a =~ r/\W_//g;

    There is no /g modifier for the tr/// operator. The transliteration operator also does not use regexp-like predefined character classes, such as \W. You probably meant something like:

    $string =~ tr/a-zA-Z0-9.//cd;

    This says to take the complement of the list created by the ranges a-z, A-Z, and 0-9, and delete anything not found among that range. Note, I didn't specifically deal with the underscore character, because with this method, the fact that I didn't include it is enough to get it deleted. Think of the list of things on the left of the tr/// operator as the list of what to keep, because we used the /c modifier. Everything else gets deleted, because we used the /d modifier.

    Updated: Added the '.' character to the "keep these" character list to accommodate the fact tht the OP revised his question later in the thread asking to also preserve the dot character.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://375221]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2018-06-19 05:36 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (111 votes). Check out past polls.