在tcl

2014-12-05 34 views
0

中模擬「exit do」語句在vba中的融合此問題與Breaking parent loop in tcl有關,但我無法實現我當前代碼的答案。在tcl

我用VBA編寫Excel的一個進程,它使用「退出做」:

Do While i < numofentries 
    Debug.Print "------------------" 
    Debug.Print "j = " & j 
    Debug.Print "length | Element i" 
    path_beg = i 
    path_end = i 
    Length = 0 
    length_l = 0 
    length_u = 0 
    blnAngle = True 
    Do While Length < minlength 
     If (angles(i) < limitangle Or path_beg = i) Then 
      Length = Length + ElemEdgeLengths(i) 
      Debug.Print Length & " | " & i 
      path_end = i 
      i = i + 1 
      If i = numofentries Then Exit Do 
     Else 
      Debug.Print "Angle change..." 
      If path_end = path_beg Then 
       path(j - 1) = path_end 
       blnAngle = False 
       Debug.Print "1 Elm only: included to predecessor path:" 
       Debug.Print "path_end of path j = " & j - 1 & " changed to " & path_end 
      Else 
       path(j) = path_end 
       j = j + 1 
       blnAngle = False 
       Debug.Print path_end - path_beg + 1 & " Elms: own path with length " & Length 
      End If 
      Exit Do 
     End If 
    Loop 
    If blnAngle = True Then 
     length_l = Abs(Length - ElemEdgeLengths(path_end - 1) - minlength) 
     length_u = Abs(Length - minlength) 
     If length_l <= length_u Then 
      i = path_end 
      path(j) = path_end - 1 
      j = j + 1 
      Debug.Print "last Elm set to " & path_end - 1 
     Else 
      path(j) = path_end 
      j = j + 1 
      Debug.Print "last Elm is " & path_end 
     End If 
    End If 
Loop 

我已經「翻譯」分到TCL這樣的,但我不能夠模擬ExitDo的行爲正確。我如何正確地重新編碼Tcl中的VBA Sub? :


proc magictrap {code body} { 
    if {$code <= 4} {error "bad magic code"}; # Lower values reserved for Tcl 
    if {[catch {uplevel 1 $body} msg opt] == $code} return 
    return -options $opt $msg 
} 
proc magicthrow code {return -code $code "doesn't matter what this is"} 



proc pathfinder {ElemEdgeLengths minlength LimitAngle angles} { 
set j 0 
set i 0 
set numofentries [llength $ElemEdgeLengths]   

    while { $i < $numofentries } {  

     set path_beg $i 
     set path_end $i 
     set length 0   
     set length_l 0 
     set length_u 0 
     set blnAngle 1 

     while { ($length < $minlength) } { 

      if { ([lindex $angles $i] < $LimitAngle) || ($path_beg == $i) } {    
       set length [ expr { $length + [lindex $ElemEdgeLengths $i] } ] 
       set path_end $i 
       incr i 
       if { $i == $numofentries } { magicthrow 5 } 
      } else { 
       if { $path_end == $path_beg } {      
        set k [expr { $j - 1 }] 
        set path($k) $path_end 
        set blnAngle 0      
       } else { 
        set path($j) $path_end 
        incr j 
        set blnAngle 0 
       } 
       set length inf 
      }       
     } 

     if { $blnAngle == 1 } { 
      set length_l [ expr { abs($length - [lindex $ElemEdgeLengths [expr {$path_end - 1} ] ] - $minlength ) } ] 
      set length_u [ expr { abs($length - $minlength) } ]  
     } 

     if { $length_l <= $length_u } { 
      set i $path_end 
      set path($j) [expr {$path_end - 1}] 
      incr j       
     } else { 
      set path($j) $path_end 
      incr j 
     } 

    } 

    return $path 

} 
+0

什麼是相當於指令'退出在tcl裏做'? – 2014-12-05 10:56:36

回答

0

「break」退出最裏面的循環。

,你可以在「退出做」的位置設置一個布爾變量:

set blnBreak 1 
    break 

外剛內循環,你將不得不測試它:

if { $blnBreak == 1 } { 
     break 
    } 
0

的VB Exit Do非常緊密對應於Tcl的(除了和許多其他語言)break

+0

不同之處在於在tcl中它只打破了當前循環,而不是在這種情況下的「外部循環」。 – Lumpi 2014-12-05 12:24:45