in reply to Re: Re: expanding the functionality of split
in thread expanding the functionality of split

If you want to keep an array with the delimiters around, just do it. And when it's time to pass it on to split, just do:
@chunks = do {local $" = "|"; split "@array" => $str};

assuming that @array is the array with your delimiters. You can of course put that in a sub:

sub mysplit (\@;@) { local $" = "|"; split "@{+shift}" => @_; } @chunks = mysplit @array => $str;

Abigail

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

    Idiomatic as always. Is there still a fat comma in Perl 6? However the key point for me is that if you are going to split on a array of delimiters you often need to sort them by length first to get the behaviour you want. The ':' '::' example is a good case in point. If the order is ':', '::' you will never split on '::' as Perl will always do the ':' split and as a result return a number of (probably) unwanted null fields if we have any instances of '::' in the split string. This also holds true in the more usual case where you are doing a match or sub (on|a|range|of|odds|and|ends). If we used that order we would never match 'range' or 'and' as we always match the 'a' - unless we applied boundary conditions, etc....

    cheers

    tachyon

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

      But sorting on length will not do. A trivial and silly example, when the delimiters are abc and ab*c*, if you try ab*c* first, you'll never succeed on abc.

      You could make an ordering if you can decide whether one regex will match everything another does. I doubt this is a decidable question for Perl regular expressions. It is for "normal" regular expressions, and, IIRC, undecidable for context free grammars. Perl regular expressions are hard to qualify in this sense, but even if it's theoretical possible, it's not going to be cheap, and hence the price would be high.

      It's going to be a responsibility of the programmer to pass in the options in a logical order; just as already is required for alternations in regular expressions.

      Abigail

        It is alway beholden upon the programmer to consider the edge cases. This particular trap is one I have found myself in several time wondering how $var_x ended up with a null value. Fully 80% of the respondents to this node also tendered code that produced this result - albeit we were providing solutions to the wrong problem!. There is a recursive solution to the actual problem that I posted here - doubtless you will be able to demonstrate a failure edge case.....but you get that....

        cheers

        tachyon

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

Re: Re: expanding the functionality of split
by BrowserUk (Patriarch) on Dec 10, 2002 at 15:07 UTC

    Isn't there a problem with this?

    What happens if one of the delimiter @array elements contains a literal '|'? Won't that screw things up?


    Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
    Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
    Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
    Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.

      It will screw up things in the same as other suggestions in the thread will screw up things. It will also screw up in the same was as using a literal '|' in current split will screw up things.

      It will also screw up things if you put in a literal ( or a [. Don't do it then! Backwack it! (Twice if you use double quotes).

      Abigail

Re: Re: expanding the functionality of split
by BrowserUk (Patriarch) on Dec 10, 2002 at 17:03 UTC

    Sorry Abigail, I can't get your neat solution to work for me, could you explain what I am doing wrong?

    #! perl -slw use strict; sub mysplit (\@;@) { local $" = "|"; #" split "@{+shift}" => @_; } my @array = (':','::','\s+'); my $string = "a:b::c d"; my @chunks = mysplit @array => $string; print @chunks; __END__ C:\test>218759.pl 1

    Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
    Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
    Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
    Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.

      Blasted prototyping in Perl. The second argument to split is taken to be in scalar context, hence the @_ becomes equivalent to 1.

      Abigail