We don't bite newbies here... much | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
I asked a question in the CB earlier today regarding testing
a string to check that it a) consisted of just one characeter,
and b) that character was in the range A to Z.
I'd already come up with the regex /^[A..Z]$/ but I realised that there was probably a much more efficient way of doing it (especially seeing as it'll be happening 1000's of times in the code I am writing). Corion suggested if (($str ge "A") && ($str le "Z")), while tilly suggested setting up a hash containing the values which I wanted to evaluate as true, then test with if (exists $chrs{$item}). I decided this was a good time to learn how to use the Benchmark module, so this is what I ran to test all 3 -
I've no idea whether the above use of rand was a good idea or not, or if I needed to add the else on to the if (it'll be there in the production code). Here are the results -
Clearly tilly's suggestion is the winner, and I get to see first hand why regex's aren't always the best solution, even if they are quicker to type :-) I don't suppose there's an even quicker solution ;-) ? Pete - who's very grateful to tilly and Corion In reply to Testing a string for a range of characters by BoredByPolitics
|
|