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

I have bar-delimited text, I want to split on the bar. When I run this:
my $line = "oneX|twoX|threeX"; my @fields = split(/|/,$line); print "$fields[1]\n";
I get "n". When I change the split call to split(/X/,$line) I get what I expect: "|two".
I even checked the text in a hex editor, and found no reason split would not split on the "|".
thanks

Replies are listed 'Best First'.
Re: difficult to split on "|"
by saintmike (Vicar) on May 14, 2005 at 03:08 UTC
    The 'pipe' sign is a meta character in perl's regular expressions, you need to escape it if you mean a literal "|":
    my @fields = split(/\|/,$line);
      Thanks. The answer is obvous now that you showed me.
Re: difficult to split on "|"
by gaal (Parson) on May 14, 2005 at 04:20 UTC
    Happily, in Perl6 /|/ is a compile-time error. (Already works that way in PGE/Pugs.)

    Note that (in P6) you can split on the string "|", which in this case would do what you want.

Re: difficult to split on "|"
by gube (Parson) on May 14, 2005 at 04:38 UTC

    Use Escape Character for the problem. 'split(/\|/'

    Regards

    s,,aaagzas3uzttazs444ss12b3a222aaaamkyae,s,,y,azst1-4mky,,d&&print

Re: difficult to split on "|"
by johnnywang (Priest) on May 14, 2005 at 06:08 UTC
    Since "|" is a meta charactor, you were actually using the empty pattern: "//", which is splitting at the null string:
    print join(",",split(//, "oneX|twoX|threeX")); __OUTPUT__ o,n,e,X,|,t,w,o,X,|,t,h,r,e,e,X
Re: difficult to split on "|"
by Anonymous Monk on May 14, 2005 at 14:54 UTC
    my @fields = split(/\|/,$line);