Don't ask to ask, just ask | |
PerlMonks |
Re: Check for Spaces in a Stringby aaron_baugher (Curate) |
on Jun 15, 2015 at 20:52 UTC ( [id://1130529]=note: print w/replies, xml ) | Need Help?? |
To check for a space followed by a word character is simple, though there are a few similar patterns that might serve your needs best:
However, since you're applying a regex here, it might be just as efficient to go ahead and do the split and then see whether it split anything. That would take a bit more time on the lines that are a single word, but less time on the ones with multiple words:
Update: I thought I'd benchmark it (code below), and found that if 50% of the values needed to be split as in the example above, the two methods were equally fast:
But when I made it so 75% of the values needed to be split, the "split everything and then check for a second element" method was the clear winner:
So it looks like if less than half your lines will need to be split, check first, then split the ones that matched. If more than half will end up being split, just split them all and check for a second element in the resulting array, and go from there. (Incidentally, checking for the second element ($v[1]) was also a gain over checking the number of elements (@v>1) as I originally did.) Here's the benchmarking code:
Aaron B.
In Section
Seekers of Perl Wisdom
|
|