in reply to Re: Re: split/map weirdness: empty strings vs undef
in thread split/map weirdness: empty strings vs undef

You'll need to escape the pipe, though, because its a special char (alternation) in the regex...

Quite so. And note that   split(/\|/, "2|3||||"); gives a different result than   split('|', "2|3||||"); Consult perlfunc for details. You probably want to be using the latter form, as it retains empty fields.

Replies are listed 'Best First'.
Re: Re: Re: Re: split/map weirdness: empty strings vs undef
by sauoq (Abbot) on Oct 04, 2002 at 20:24 UTC

    Uhm... split(/\|/, "2|3||||"); is different from split('|', "2|3||||"); only because that pipe isn't escaped in the second one. Perl will split on that as if it were a pattern. In other words, that's broken in the same way as your original one. Note that splitting on a literal space is a special case. This isn't.

    $ perl -le '$_="2|3|||||"; @a = split q(|); print "($_)" for @a' (2) (|) (3) (|) (|) (|) (|) (|) $ perl -le '$_="2|3|||||"; @a = split /|/; print "($_)" for @a' (2) (|) (3) (|) (|) (|) (|) (|)
    -sauoq
    "My two cents aren't worth a dime.";
    
      print "($_)\n" for split(/\|/, "2|3|||||"); __END__ (2) (3) print "($_)\n" for split('|', "2|3|||||"); __END__ (2) (|) (3) (|) (|) (|) (|) (|) C:\> perl -v This is perl, v5.6.1 built for MSWin32-x86-multi-thread (with 1 registered patch, see perl -V for more detail) Copyright 1987-2001, Larry Wall Binary build 631 provided by ActiveState Tool Corp. http://www.ActiveS +tate.com Built 17:16:22 Jan 2 2002

        Yes. That's exactly my point. My examples showed that /|/ and '|' were the same. For completeness:

        $string = "2|3||"; print "Backwhacked bar in pattern: "; print "($_)" for split /\|/, $string; print "\n"; print "Backwhacked bar in quotes: "; print "($_)" for split '\|', $string; print "\n"; print "Just a bar in pattern: "; print "($_)" for split /|/, $string; print "\n"; print "Just a bar in quotes: "; print "($_)" for split '|', $string; print "\n"; __END__ Backwhacked bar in pattern: (2)(3) Backwhacked bar in quotes: (2)(3) Just a bar in pattern: (2)(|)(3)(|)(|) Just a bar in quotes: (2)(|)(3)(|)(|)

        The presence of bars in the output of both splitting on '|' and on /|/ is because those are both really splitting on the null pattern. The quoted bar is not preserving fields.

        -sauoq
        "My two cents aren't worth a dime.";