Come for the quick hacks, stay for the epiphanies. | |
PerlMonks |
Re^5: how to find this substring?by GrandFather (Saint) |
on Jun 10, 2012 at 05:04 UTC ( [id://975370]=note: print w/replies, xml ) | Need Help?? |
Write it correctly the first time Yes, exactly. Then dirty it up to make it go faster if you must. Your sample code is an excellent case in point. If you were processing vast numbers of strings in a highly time critical application and were doing no other processing at all then there may be some justification for using index instead of a regex match and performing the extra checking required to detect a failed match. Otherwise it's a lot clearer to go with the simple regex. It doesn't help your argument that your unchecked substr sample code gives what would probably be considered the wrong answer when there is no match: the last character of the string. Correct handling of the no match case dirties up the index code and doubles its execution time, but the humble regex is essentially unaffected. Consider:
Prints:
True laziness is hard work
In Section
Seekers of Perl Wisdom
|
|