dbohdan 2014-06-15: The following procedure removes items at both ends of a list that match a regexp. By default this amounts to removing empty items.
# Remove empty items at the beginning and the end of a list. proc ltrim {list {emptyRegExp "^$"}} { set first [lsearch -not -regexp $list $emptyRegExp] set last [lsearch -not -regexp [lreverse $list] $emptyRegExp] return [ if {$first == -1} { list } else { lrange $list $first end-$last } ] }
Use example
eltclsh > ltrim {{} {} a b c {} 1 2 {} {} {} {} {}} a b c {} 1 2 eltclsh > ltrim {{ } {} a b c { } 1 2 { } {} " \t " {} "\t\t"} {^[ \t]*$} a b c { } 1 2
See also