Recently, someone posted in a Perl forum a problem which essentially could be simplified to this:

We have a string consisting of digits and dots. The first and last characters are guaranteed to be digits, and there are at least two digits between every two dots. Example: '123.45.678.9' Problem: Turn this string into a list of decimal numbers, each number having exactly one digit after the decimal point. In our example, this would be the list (123.4, 5.6, 78.9)

The fellow tried to solve this using split and failed. I suggested a solution using a pattern //g, which is IMO the more natural way to solve the problem.

However, I found the task interesting enough to think whether there also exists a solution involving split. In this case, we don't have fields where to split the string - or, to be more precise, the split points are zero-length. Hence, I thought, this could be solved using a negative look-ahead assertion. So I tried this:

perl -lwe 'use strict; print(join("/",split(/(?!\.\d)/,"123.456.78.1" +)))'
The idea is that a split point is any point in the string which is preceeded by a dot followed by a digit. To my surprise, this resulted in
1/2/3./4/5/6./7/8./1
to be printed. Could someone kindly explain this result?

-- 
Ronald Fischer <ynnor@mm.st>

In reply to split on zero-length pattern by rovf

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.