Fun. Some edge cases got missed several times. Strings under 12 characters and a first word of over 12 characters were the most common, I think. Though what to do when the first word is over 12 characters is not clear from the problem statement. Strict interpretation of the problem statement would end up with an empty string for that case. More likely, the first 12 characters of the first word is appropriate.
But I think you can still do this and keep it pretty simple:
my( $first )= $message =~ /^\s*(.{1,12}\s|.{0,12})/;
Many ways to do this. For such a tiny, simple operation, I could envision a half-dozen tests in the UT suite off the top of my head. (:
Note that I don't collapse internal spaces, which is a nice touch in Transient's solution.
- tye
In reply to Re: Splitting a string into words
by tye
in thread Splitting a string into words
by webchalkboard
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |