Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: replacing pipe "|"

by thpfft (Chaplain)
on Mar 26, 2003 at 20:45 UTC ( [id://246076]=note: print w/replies, xml ) Need Help??


in reply to replacing pipe "|"

you need to escape the pipe, which sounds a lot more exciting than it is:

s/\|/ /go;

will work. | is a control character in a regex, unless preceded by the escaping backslash.

update goodness. that's a new peak of redundancy. I was too busy wondering why s// /g does what it does. Can anyone explain?

tachyon yeah, it's the op's /o, and i forgot to take it out when i simplified the reply.

Replies are listed 'Best First'.
Re: Re: replacing pipe "|"
by tachyon (Chancellor) on Mar 26, 2003 at 22:04 UTC

    You do know that /o tells perl that any $scalar_var in the regex will never change its value so only compile the regex once don't you? If so why use it whan all you have is literals in your example?

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Re: replacing pipe "|"
by Nkuvu (Priest) on Mar 26, 2003 at 21:03 UTC

    s// /g replaces all null characters with a space. And it so happens that a null character is between every other character, so...

    And by the way, s/|/ /g means replace either null or null with a space. :)

    Does this answer your question?

    Update: The 'null character' terminology isn't correct. See BrowerUK's note.

      not really, but it restates it nicely enough. it's the so-happens part which intrigues me. why is there a null character there, and why only one?

        This is a misnomer. There isn't a "null character" between every character.

        There couldn't be, else there would need to be a null character--usually defined as chr(0) or "\x00" or "\0" etc.--between each null character and the adjacent non-null character. Then there would need to be a null character between each pair of adjacent null characters... As you can see that is recursive and every string would be of infinite length;)

        Possibly a better way of stating it is that the null-length string--defined as a string of length 0 (zero) bytes--is deemed to exist between each pair of characters in a string. Also before the first character and after the last character.

        The regex s// /g will 'find the null-length strings between each character' and 'replace them' with spaces.

        $s= 'ABCDEFG' $s =~ s// /g print "'$s'" ' A B C D E F G '

        Viewed this way, ie. as a null-length string, then it makes a certain amount of sense. If you remove the first character and the last character from a two character string, the what you have left is a null-length string. Therefore, it could be viewed that a null-length string exists between the first and last characters of the two character string. It's easy to see that you can extend this logic to say that it exists before and after every character in the string.

        Whether this 'deemed to exist'ness of the null-length string has any useful properties is an open question. You can do thing like:

        $s= 'ABCDEFG' $s =~ s/^/prefix-/ print $s prefix-ABCDEFG $s =~ s/$/-postfix/ print $s prefix-ABCDEFG-postfix

        but it's easy to see that either of these is easily achieved by concatenation.

        I've just racked my brains--for at least 5 minutes:)--to find an example where matching for the null-length string is useful, but so far I've failed. Conversely, I have encountered many occasions when I've had to go to extra and sometimes extreme lengths to prevent a regex matching the null string in order to achieve my aims. I often wish that if I construct a regex that leads the engine to conclude that I want to match a null string, that it would issue a warning, and (optionally?) die.

        Maybe someone else will offer an example of where matching a null-length string is useful.


        Examine what is said, not who speaks.
        1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
        2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
        3) Any sufficiently advanced technology is indistinguishable from magic.
        Arthur C. Clarke.

        Technically it's not a null character -- I think I mis-named it. It's a zero-width assertion, like \b. From the Camel:

        Notice that a tab is one character wide, while a word boundary is zero characters wide because it's the spot between two characters. So we call \b a zero-width assertion.

        As far as further explanation goes, perhaps someone who is more of an expert at this (which is a large number of people ;) ) can step in...

Log In?
Username:
Password:

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

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

    No recent polls found