2016-03-14 49 views
0

排序列表。如果List1是:TCL:取決於陣列

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 

而且array1是:

{3 4 5} {12 13} {20 21} 

如何根據array1轉換list1通過替換array1每個元件的反向列表即產生這樣的輸出:

1 2 5 4 3 6 7 8 9 10 11 13 12 14 15 16 17 18 19 21 20 22 23 24 25 
^^^^^    ^^^^^     ^^^^^ 

回答

2

這不是一個排序任務,這是一個搜索任務。

如果假設範圍扭轉不重疊,但不一定存在兩種(即使用的事實,他們是連續的數字),你得到的東西是這樣的:

# Iterate over each of the replacement patterns 
foreach range $array1 { 
    # Iterate over each of the locations where the first element of the current 
    # replacement pattern is found 
    foreach pos [lsearch -all -exact $list1 [lindex $range 0]] { 
     # This will be the index of the *last* element in each subrange 
     set pos2 [expr {$pos + [llength $range] - 1}] 

     # Do the reversed replacement if the ranges match 
     if {[lrange $list1 $pos $pos2] eq $range} { 
      set list1 [lreplace $list1 $pos $pos2 {*}[lreverse $range]] 
     } 
    } 
} 

此後的結果將在更新的list1變量中。將程序包裝成一個練習。