Re: Tcl Switch case



On Feb 21, 9:10 am, Bryan Oakley <oak...@xxxxxxxxxxxxxxxxxxxx> wrote:
tom.rmadilo wrote:
switch ?options? word pattern body pattern body ....

That's not a new form. It's been around since version 7.6.

http://www.tcl.tk/man/tcl7.6/TclCmd/switch.n.html

Eeek! You are right. Going back to the 'mess' which is switch. This is
a unique command in several ways. First is that it is a control
structure. I'll have to look, but I don't know of any other Tcl flow
control commands which take options. But something else was added to
this mess in 8.5: options which take values! the -matchvar and -
indexvar options greatly complicate the situation. In many Tcl
commands I have argued that switches help unite common operations into
a single whole, but in the case of switch, the options seem to have
the opposite effect. The options have a drastic effect on the intended
operation.

And unless you are the unfortunate maintainer of the code, or charged
with creating the byte code for this monster command, it doesn't look
like too much of an issue. To visualize the difficulty to myself, I
just wrote up a replacement ensemble command for [switch]. To handle
the previous 8.4 options, there are six distinct subcommands, although
I broke these into two categories (nocase and not nocase):


proc ::myswitch::exact { word body } {
uplevel 1 [list switch -exact -- $word $body]
}

proc ::myswitch::glob { word body } {
uplevel 1 [list switch -glob -- $word $body]
}


proc ::myswitch::regexp { word body } {
uplevel 1 [list switch -regexp -- $word $body]
}

The nocase variety does the same thing in the ::myswitchnc namespace.
I know this is ugly, probably doesn't work in every case, the point is
illustration of the complexity, and what it would take to reduce this
to the 'no option parsing' case.

With 8.5, you add the following subcommands:


proc ::myswitch::regmvar { matchvar word body } {
uplevel 1 [list switch -regexp -matchvar $matchvar -- $word $body]
}

proc ::myswitch::regivar { indexvar word body } {
uplevel 1 [list switch -regexp -matchindex $indexvar -- $word
$body]
}

proc ::myswitch::regmivar { matchvar indexvar word body } {
uplevel 1 [list switch -regexp -matchvar $matchvar -indexvar
$indexvar -- $word $body]
}

So the number of subcommands has doubled from six to 12, and also
reduced the opportunities for compiling code.

The ensemble command:

namespace eval ::myswitch {
namespace export exact glob regexp regmvar regivar regmivar
namespace ensemble create
}

.



Relevant Pages

  • Re: After I overload "switch" command, how can I get back the original "switch" function back?
    ... before creating the replacement procedure. ... rename switch switch_savedForLaterByPheobe ... If the code that is using your modified is in a namespace other ... # Invoke command that uses the custom ...
    (comp.lang.tcl)
  • Re: C2960 config - What do the commands do
    ... To enable visible sequence numbering of system logging messages, use the service sequence-numbers command in global configuration mode. ... Configure the system to automatically switch to summer time. ... This sybnet has the same network address what main network (the subnetted one) but longer subnet mask ... For chassis with 64 MAC addresses, STP uses the extended system ID plus a MAC address to make the bridge ID unique for each VLAN. ...
    (comp.dcom.sys.cisco)
  • Re: proposal: reswitch
    ... > which levels of loops and/or switches and/or ... socket -server command ?-myaddr addr? ... You've got the quick-exit (break, no string), the fall-through to next ... -switch option), then it'd be the nth switch. ...
    (comp.lang.tcl)
  • voice activated Windows shortcuts
    ... It works if the command pause is set to .1 seconds and the ... dictation while in Command Mode and AFAIK there's no hotkey to switch ... switching between windows does not ... back space (click, Backspace, Space) ...
    (comp.speech.research)
  • voice activated Windows shortcuts
    ... It works if the command pause is set to .1 seconds and the ... dictation while in Command Mode and AFAIK there's no hotkey to switch ... switching between windows does not ... back space (click, Backspace, Space) ...
    (comp.speech.users)