2013-04-14 82 views
1

想,如果我有一個大名單看起來像:如何從另一個列表中減去一個列表並檢查一個模式的順序?

一個

ab1 ab2 ab3 ab5 ab6 ac1 ac2 ac3 ac4 ac5 ac6 ac7 ac8 ac9 xy1 xy2 xy3 xy4 xy5 xy6 xy7 xy8 xy9 xy10 xy11 

ac1 ac4 ac5 ac6 ac7 ac8 xy2 xy3 xy4 xy5 xy6 

A之後 - B模式變得像:

ab1 ab2 ab3 ab5 xy1 ab6 ac2 ac3 xy7 xy9 xy8 xy10 xy11 ac9 

現在我有,使之成爲

ab1 ab2 ab3 ab5 ab6 ac2 ac3 ac9 xy1 xy7 xy8 xy9 xy10 xy11 

所有值進行排序,然後我需要檢查等所有序貫價值的存在,我得到的第1和序列的LST值。在上述情況下

ab1 ab2 ab3 are in sequnce so for this part 1st value = ab1 and last value = ab3 
ab5 ab6 --- first value = ab5 and last value ab6 
ac2 ac3 --- first value = ac2 and last value = ac3 
ac9 --- first value and last value same = ac9 
xy1 --- first value and last value same = xy1 
xy7 xy8 xy9 xy10 xy11 --- first value = xy7 and last value = xy7 and last value = xy11 

:在列表或陣列的所有值都是唯一

回答

1

礦不是很漂亮,但它的伎倆^^;

set group [list "ab1" "ab2" "ab3" "ab5" "ab6" "ac2" "ac3" "ac9" "xy1" "xy7" "xy8" "xy9" "xy10" "xy11"] 

set number 0 
set pattern 0 
set result "" 

foreach n $group { 
    if {$pattern == 0} { 
     set current $n 
     lappend result $n 
     regexp {([a-z]{2})(\d+)} $n - pattern number 
     continue 
    } 
    regexp {([a-z]{2})(\d+)} $n - match1 match2 
    if {$match1 == $pattern && [incr number] == $match2} { 
     set current $n 
     continue 
    } else { 
     set pattern $match1 
    } 
    if {[lsearch $result $current] == -1} { 
     lappend result $current 
    } 
    lappend result $n 
    set current $n 
    set number $match2 
} 
lappend result $n 

# $result = "ab1 ab3 ab5 ab6 ac2 ac3 ac9 xy1 xy7 xy11" 
1

如果您正在使用的Tcl 8.6,那麼lmap是第一項任務的合適的工具:

set filtered [lmap e $A {if {$e in $B} continue}] 

排序:

set sorted [lsort $filtered] 

而最後一個任務..好,讓我們用第一2個字符。

set filter {} 
set last {} 
foreach e $sorted { 
    if {[string range $e 0 1] ne $filter} { 
     lappend res $last $e 
     set filter [string range $e 0 1] 
    } 
    set last $e 
} 
lappend res $last 
set res [lrange $res 1 end] 

現在您有一個列表中的第一個和最後一個元素。

相關問題