There's more than one way to do things | |
PerlMonks |
Re^2: Avoiding if/else knotsby xdg (Monsignor) |
on Aug 18, 2006 at 13:28 UTC ( [id://568142]=note: print w/replies, xml ) | Need Help?? |
The trouble with dispatch tables in Perl is that the conditions need to be exact keys. In other words, if you want any value in the range 1-999 to do the same thing, you need to create keys for each of those integers. If you need integers in a range, you've still got a discrete set of values and mapping that to the dispatch table isn't hard.
The challenge is for non-integer conditions or non-bounded ones. What if N > 1000? What if N = 3.14159265358979323? What if N = 'PI'? In that case, you still need to have the advance logic, if/else or otherwise, to convert inputs to a smaller number of well-defined cases. My advice in that case is keep the case logic in one place and the actions all somewhere else via subroutines. Dispatch tables probably don't help unless you're already starting with a discrete, bounded set of inputs (or need to dynamically add cases as your program runs). -xdg Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.
In Section
Seekers of Perl Wisdom
|
|