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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.