You're right. I prefer this approach as it lets perl do the job of building and keeping track of the list internally - that's what "more merit" was referring to. The original version of this approach did not have the grep, and I didn't think about the fact I was introducing a second loop when I added it.
I tried to get rid of it as follows:
#!/usr/bin/perl -wl use strict; sub msplit { my ($delim, $str) = @_; my $pat; ### $pat = q/ (?> (.*?) (??{ shift @$delim }) (??{ $pat }) )? /; ### U +PDATE: WRONG $pat = q/ (?> (.*?) $$delim[0] (??{ shift @$delim; $pat }) )? /; $str =~ /^ $pat (.+) /x; } print for map "'$_'", msplit [qw(: :: \s+)], "a:b::c d";
I like this version even better as it should be even more economical: the first failed match bails out of the pattern so it does not do any more work on building the regex than necessary.
Unfortunately Perl complains about the lack of use re 'eval'; even if I add it. I'm not quite sure as to what's missing.. Maybe one of our resident regex spell casters can enlighten me?
Makeshifts last the longest.
In reply to Re^3: expanding the functionality of split
by Aristotle
in thread expanding the functionality of split
by tigervamp
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |