2

Suppose if i have a big list which looks like :

A

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

and

B

ac1 ac4 ac5 ac6 ac7 ac8 xy2 xy3 xy4 xy5 xy6 

after doing A - B pattern becomes like :

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

Now i have to sort all values so that it becomes

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

then i need to check the existence of all sequencial values such that i get the 1st and lst values of the sequence.In the above case

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

NOTE:All values in the list or array are unique

munish
  • 4,505
  • 14
  • 53
  • 83

3 Answers3

2

If you are using Tcl 8.6, then lmap is the right tool for the first task:

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

Sorting:

set sorted [lsort $filtered]

And the last task.. good, let's use the first 2 characters.

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]

Now you have a list with the first and the last element.

Johannes Kuhn
  • 14,778
  • 4
  • 49
  • 73
1

Look at struct::set and struct::list packages of tcllib.

kostix
  • 51,517
  • 14
  • 93
  • 176
1

Mine is not very pretty, but it does the trick ^^;

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"
Jerry
  • 70,495
  • 13
  • 100
  • 144