in reply to Re: optimized switch case
in thread optimized switch case

thank you,
That worked like a dream. I did have one follow-up question though.In my case, I just had 3 choices which the user could choose from.What if there were a dozen or more choices which could be selected in any combination...hard-coding an array for that scenario would be very cumbersome, wouldnt it ? is there a separate approach to take in the case ?

Replies are listed 'Best First'.
Re: Re: Re: optimized switch case
by Zaxo (Archbishop) on Jan 04, 2002 at 03:51 UTC

    Yes, it gets very unwieldy for larger numbers of options. That is unavoidable if the options are mutually dependent and each combination must give a unique response. That is probably a sign to refactor what the options represent.

    If you really have that many distinct choices, chances are they are independent enough to benefit from the treatment suggested by Fastolfe and clintp. The numeric mapping I suggested doesn't need to be used as an array index. An option number can be tested with bitwise operators when logic is needed, and magically masked numbers can be used as hash keys.

    After Compline,
    Zaxo