G'day swampyankee,

"I was expecting ... "SMITH", "-", "JONES" ... I got "S","","M","", ... What did I do wrong?"

The documentation for split describes what happens with capturing. The section at the end (starting with "If the PATTERN contains capturing groups, ...") has a description followed by several examples.

Here's your regex without capturing:

$ perl -E 'say "|$_|" for split /-| /, "A B-C"' |A| |B| |C|

Now with capturing (and what I think you intended):

$ perl -E 'say "|$_|" for split /(-| )/, "A B-C"' |A| | | |B| |-| |C|

If you coded /(-|)/ instead of /(-| )/, you would get the output you're seeing:

$ perl -E 'say "|$_|" for split /(-|)/, "A B-C"' |A| || | | || |B| |-| |C|

That, of course, is just a guess; however, given other issues (already noted by hippo) in your posted code, possibly a good guess.

"The split's documentation seems to say that / / doesn't split between every character, but " " does."

I expect you've misread or misunderstood something. Had you quoted the text that you thought seems to say what you suggest, I could comment further. There can be errors in documentation and those errors can be fixed; perhaps there's not an error but a clarification of the current text would help — obviously, the source of the confusion needs to be identified as a first step.

Anyway, neither / / nor " " will "split between every character":

$ perl -E 'say "|$_|" for split / /, "A B-C"' |A| |B-C| $ perl -E 'say "|$_|" for split " ", "A B-C"' |A| |B-C|

Without the spaces, both will "split between every character":

$ perl -E 'say "|$_|" for split //, "A B-C"' |A| | | |B| |-| |C| $ perl -E 'say "|$_|" for split "", "A B-C"' |A| | | |B| |-| |C|

Regardless, I don't see how / / or " " relate to /(-| )/.

— Ken


In reply to Re: Split confusion by kcott
in thread Split confusion by swampyankee

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.