more useful options | |
PerlMonks |
Re: Can you spot the problem? (solution)by dws (Chancellor) |
on Mar 05, 2004 at 23:09 UTC ( [id://334403]=note: print w/replies, xml ) | Need Help?? |
It's no secret at this point that the "problem" is the ORing of strings instead of numbers. This code fragment derives from discussion about an interview question that asked for a code fragment for determining if a dotted quad IP address was valid. One straightforward solution was basically
which works, though there are better ways to parse IPv4 addresses. In the course of "improving" the solution, someone reached into their bag of tricks and pulled out one often used in assembly language (and occassionaly in C): OR the numbers together and compare once, thus trading off 3 tests and branches against less expensive boolean operations. Unfortunately, Perl's automagic string to number conversion then doesn't happen, and the "improved" expression ORs strings instead of numbers. But, it looks like it should work! And, in one of those amusing quirks that happen now and then, you might not notice the problem if you happened to pick a simple set of test data, such as this one, which tests boundaries and then probes into the middle of the range.
My solution was to force the conversion via
which tye hinted at above (and pointed out privately) could more simply be written as
In Section
Meditations
|
|