in reply to optimized switch case

You could map the options into 1, 2, and 4, then add them. That will be insensitive to permutations, and provide a unique number for each combination. The selection can just use the number as an array index.

my @selections = ( 'Feeblizer', 'Cervical Vorbalizer', 'Cortical Vorbalizer', 'Corticocervical Extravorbalizer', 'Lumbar Vorbalizer', 'Cervicolumbar Extravorbalizer', 'Corticolumbar Extravorbalizer', 'Hypervorbalizer' ) $_ = $optstring # wherever that comes from my $options = m/A/ + 2 * m/B/ + 4 * m/C/; print $selections[$options];

After Compline,
Zaxo

Replies are listed 'Best First'.
Re: Re: optimized switch case
by screamingeagle (Curate) on Jan 04, 2002 at 00:25 UTC
    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 ?

      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