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

I am having a terrible time stripping the pipe character out of a string. I have done everything i can think of short of ripping the pixels out of the monitor.

Any help would be great.

$value = " | " #note i am not actually preloading this

s/'|'//;

s/"|"//;

s/|//;

I thinks its because pipe is a special character, but i don't know how its classified as. -thanks

  • Comment on Trying to strip the Pipe "|" character from a string

Replies are listed 'Best First'.
Re: Trying to strip the Pipe "|" character from a string
by davido (Cardinal) on Jun 10, 2004 at 17:43 UTC
    The pipe character has special meaning in regexps: alternation. That means you need to escape it with a backslash like this:

    s/\|//g;

    Or use the tr/// operator:

    tr/|//d

    There's good information on the subject in perlrequick, perlretut, perlop, and perlre. ...lots of reading, I know. But it's helpful.


    Dave

Re: Trying to strip the Pipe "|" character from a string
by hardburn (Abbot) on Jun 10, 2004 at 17:44 UTC

    I thinks its because pipe is a special character

    Yes, that is the problem. Whenever you encounter a special character, you escape it:

    s/\|//;

    But since you're just getting rid of this character, it's faster to use tr///:

    tr/|//d;

    IIRC, tr/// does't require you to escape ''|", because its special chars are more limited.

    ----
    send money to your kernel via the boot loader.. This and more wisdom available from Markov Hardburn.

Re: Trying to strip the Pipe "|" character from a string
by calin (Deacon) on Jun 10, 2004 at 17:50 UTC
    In addition, you need to bind the s/// (or tr///) operator to the variable $value. A bare s/// or tr/// works on the special variable $_. Example:
    $value =~ s/\|//g;