rovf has asked for the wisdom of the Perl Monks concerning the following question:
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:
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 inperl -lwe 'use strict; print(join("/",split(/(?!\.\d)/,"123.456.78.1" +)))'
to be printed. Could someone kindly explain this result?1/2/3./4/5/6./7/8./1
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: split on zero-length pattern
by tinita (Parson) on Nov 26, 2010 at 10:59 UTC | |
by rovf (Priest) on Nov 26, 2010 at 11:16 UTC | |
by tinita (Parson) on Nov 26, 2010 at 11:30 UTC | |
by rovf (Priest) on Nov 26, 2010 at 12:39 UTC |