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

Here's a non-recursive function:
sub wierd_split (\@@) { my $regex = shift; local $_ = @_ ? shift : $_; my @res; ($res [@res], $_) = split shift @$regex, $_, 2 while @$regex && le +ngth; push @res => $_ if length; @res } print "Got $_\n" for wierd_split @{[':', '::', '\s+']} => 'foo:bar::ba +z frog'; __END__ Got foo Got bar Got baz Got frog

Adding all the funny stuff of split, like using it in void context, its third parameter, or captures in the regex is left as an exercise for the reader.

Abigail

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

    This gets away with being non recursive only by dint of doing the recurse through a while loop on a single line. I would call that a technicality! Instructional as always.

    cheers

    tachyon

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