in reply to expanding the functionality of split

Maybe I'm not following you, but can't you just do: @fields = split(/:|::|\s+/, $string);In other words, just use alternation in the pattern?

Replies are listed 'Best First'.
Re: Re: expanding the functionality of split
by tachyon (Chancellor) on Dec 09, 2002 at 23:49 UTC

    This will actually fail because it will split on ':' before '::' and thus return unwanted null fields. You need to do /::|:|\s+/. See below....

    $string = "a:b::c d"; @fields = split(/:|::|\s+/, $string); print "Got '$_'\n" for @fields; __DATA__ Got 'a' Got 'b' Got '' Got 'c' Got 'd'

    cheers

    tachyon

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

      You need to do /::|:|\s+/.

      And you could shorten that to just /::?|\s+/

        Maybe I did not explain what I meant properly. I don't want to match ':' OR '::' OR ..., I want to match ':' THEN '::' THEN... I want to effectively change what the split operator uses as the splitting pattern mid-split. After the first field is found using the first delimeter pattern, it is discarded and the next field can only be delimited with the next pattern. The patterns move from left to right and MUST match in the order that they are presented in.

        tigervamp