For creating an array of substrings, split is probably fastest.

However, you usually want all those substrings so you can do something else with them. If you can process the string in place, it may be faster to do something like:

c:\@Work\Perl\monks>perl -wMstrict -le "my $consonants = qr{ [^aeiouAEIOU]+ }xms; ;; my $s = qq{four score\nand seven\nyears ago\nour fathers\n}; print qq{>$s<}; ;; my $offset = 0; my $pos; while (0 <= ($pos = index $s, qq{\n}, $offset)) { substr($s, $offset, $pos) =~ s{ ($consonants) }{\U$1}xmsg; $offset = ++$pos; } print qq{>$s<}; " >four score and seven years ago our fathers < >FouR SCoRe aND SeVeN YeaRS aGo ouR FaTHeRS <

Update 1: The original code in my reply doesn't do what I imagined (update: the substr length operand is incorrect). Here's code that works as I intended and has a debug print to illustrate what's happening:

c:\@Work\Perl\monks>perl -wMstrict -le "my $consonants = qr{ [^aeiouAEIOU]+ }xms; ;; my $s = qq{four score\nand seven\nyears ago\nour fathers\n}; print qq{>$s<}; ;; my $offset = 0; my $pos; while (0 <= ($pos = index $s, qq{\n}, $offset)) { printf qq{==%s== \n}, substr($s, $offset, $pos-$offset); substr($s, $offset, $pos-$offset) =~ s{ ($consonants) }{\U$1}xmsg; $offset = ++$pos; } print qq{>$s<}; " >four score and seven years ago our fathers < ==four score== ==and seven== ==years ago== ==our fathers== >FouR SCoRe aND SeVeN YeaRS aGo ouR FaTHeRS <

Update 2: This trick (update: i.e., substr returns an lvalue) also works if you want to pass a substring alias to a processing function, but the function has to operate directly on the aliased subroutine argument element in order to preserve the aliasing "chain". Also with an illustrative print point:

c:\@Work\Perl\monks>perl -wMstrict -le "my $consonants = qr{ [^aeiouAEIOU]+ }xms; ;; my $s = qq{four score\nand seven\nyears ago\nour fathers\n}; print qq{>$s<}; ;; my $offset = 0; my $pos; while (0 <= ($pos = index $s, qq{\n}, $offset)) { process(substr $s, $offset, $pos-$offset); $offset = ++$pos; } print qq{>$s<}; ;; sub process { printf qq{==%s== \n}, $_[0]; $_[0] =~ s{ ($consonants) }{\U$1}xmsg; } " >four score and seven years ago our fathers < ==four score== ==and seven== ==years ago== ==our fathers== >FouR SCoRe aND SeVeN YeaRS aGo ouR FaTHeRS <


Give a man a fish:  <%-{-{-{-<


In reply to Re: Fastest split possible (updated x2) by AnomalousMonk
in thread Fastest split possible by pedrete

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.