in reply to Avoiding if/else knots II

I still don't want to have to construct a table with around, say, 60 combinations of which only around, say, 20 are distinct.
I'd also take the approach to normalize the 60 posibilities, i.e. coerce them into unique keys in the dispatchtable, with e.g.
$key = 'default'; $case =~ s/$reg2// && ($key = 'a'); $case =~ s/$reg1// and do { whatever(); $key = 'b' }; ...
and finally dispatch via a table
$dispatch{$key}->(@args);
Can anyone see any possible drawbacks to this approach?

Not yet, since neither context nor implementation are visible...

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Replies are listed 'Best First'.
Re^2: Avoiding if/else knots II
by Skeeve (Parson) on Aug 30, 2006 at 09:32 UTC

    I'm asking myself what's the advantage of first mapping values to keys and then dispatching the keys via a hash table instead of skipping the key and dispatching directly.

    So in all the cases where you have a $key= somethin you could also write down a call of a subroutine handling this key. Except for the default case of course.

    I'd write your example like this:

    SWITCH: for ( $myValue ) { /$reg2/ && do { sub_for_key_a(); last SWITCH; }; /$reg1/ && do { whatever(); sub_for_key_b(); last SWITCH; }; #default sub_for_default(); } # END SWITCH

    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
      TIMTOWTDI - I wanted to give an example of the m// && do {} idiom which was missing from the OP's previous thread. Since I don't know about the specifics, I wrote it somewhat generic. Depending on cases the $key or $case could be modified etc.

      As for your example, what good is it to use a labelled loop for a list containing one variable? ;-)

      --shmem

      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
        As for your example, what good is it to use a labelled loop for a list containing one variable? ;-)

        That's easy.

        1. The label is there to show that the block is emulating as a SWITCH statement.
        2. The label is also there to show what the last statements refer to.
        3. the for with just one variable is simply there to assign $value to $_ without explicitly doing it
        4. it's also there to keep the original content of $_ outside the SWITCH ("loop"). Try this
          my $v="V"; $_="_"; for ( $v ) { print "inside: $_\n"; } print "outside: $_\n";
        5. It looks good ;-) (to me). I read this as "switch for value ..."

        s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
        +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e