laziness, impatience, and hubris | |
PerlMonks |
Re: split versus =~by kcott (Archbishop) |
on Nov 10, 2022 at 20:20 UTC ( [id://11148117]=note: print w/replies, xml ) | Need Help?? |
G'day russlo, Welcome to the Monastery. "My question is: why?" As others have pointed out, without any data, we can't really answer that. Here are a few possible reasons (non-exhaustive list):
For future reference, please provide a "Short, Self-Contained, Correct Example" and follow the guidelines in "How do I post a question effectively?". "Additionally: what can I do to provide the correct splitting that we're looking for here?" Comment your regex in full. By forcing yourself to document exactly what your regex does, you will more easily spot logic errors and typos. By writing your regex as I've done in the code below, it's very easy to make changes (e.g. at some future point perhaps Z can be negative or the string becomes "W-X-Y-Z"); fiddling around inside a regex which is jammed into a single string with no whitespace is highly error-prone. As others have already suggested, write a test script. In the code below, I added an "expect failure"; mostly to show you what that outputs. I also noted you mentioned a problem with '-2-3-4'; to be honest, I didn't follow what the problem was, but I added it for testing anyway. Add more tests if you encounter problem input that isn't handled by the regex; you may also need to alter the regex itself if it doesn't cover all eventualities. Note that with the way I've written the code, you can just add to @tests without needing to change any other part of the code. You should also provide some validation and error reporting. What happens if the input doesn't match the regex? — on-screen warning? logfile entry? kill the script? Here's my test script:
And here's the output:
— Ken
In Section
Seekers of Perl Wisdom
|
|