Downey_HUff
Downey_HUff

Reputation: 189

can we swap the order of execution (top to bottom changed as bottom to top) based on flag?

i have an if loop loop where i configure commands in router as

    mode contains config or unconfig as values

if {[regexp -nocase {^config$} $mode]} {

set conf ""

} else {

 set conf no

}

 $router config "$conf primary command\n"

 $router config "$conf secondary command\n"

when mode is set to config everthing is fine , primary command is configured first and then secondary command" but while unconfiguring i want secondary command to executed first and then only it allows me to remove primary command...so when $mode changes will i be able to swap the order of execution?

Upvotes: 0

Views: 144

Answers (3)

potrzebie
potrzebie

Reputation: 1798

proc myConf {cmd} {
  global myConfs router

  $router config "$cmd\n"
  lappend myConfs $cmd
}

# configure
set myConfs {}
myConf "primary command"
myConf "secondary command"

# unconfigure
foreach cmd [lreverse $myConfs] {
  $router config "no $cmd\n"
}

Upvotes: 0

Donal Fellows
Donal Fellows

Reputation: 137787

You could try having your code keep a list of operations to perform as the user lists them instead of executing them immediately, only actually performing the operations on some signal. Then swapping the order of execution is just a matter of processing the list in the reverse direction, which is pretty trivial (and lreverse might help).

I'd not personally be inclined to do it though. The usual Tcl way is to do things immediately and in the order specified; the only notable exceptions to this are Tk (which postpones display and layout “until idle”, i.e. when there are no pending events) and Expect in some uses (and that's partial; you can expect many things at once, but they're checked for in the order specified).

Upvotes: 1

Tomer Arazy
Tomer Arazy

Reputation: 1823

Something like that?

if {$conf == "no"} {
    $router config "$conf primary command\n"
    $router config "$conf secondary command\n"
} else {
    $router config "$conf secondary command\n"
    $router config "$conf primary command\n"
}

Upvotes: 0

Related Questions