how to remove emty elements from tcl list

2019-06-25 12:28发布

hi I have following list

% set qprList {{{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} 12345 {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} 12345 {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} 12345 {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} 12345 {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}} {{}}}

I want to remove all the elements which are empty. since list of list is i am not able to do it in single loop interation.

Any simple way to achieve this?

标签: list null tcl
4条回答
Animai°情兽
2楼-- · 2019-06-25 13:08

I also use struct::list because I am lazy:

package require struct::list
set non_empty [struct::list flatten -full $qprList]
查看更多
贼婆χ
3楼-- · 2019-06-25 13:09

There are no empty elements in that list. The ones that seem empty can be considered as (a) a string "{}" or (b) a list with one element which is an empty string or an empty list.

package require struct::list
set non_empty [struct::list filter \
                 [struct::list flatten $qprList] \
                 {apply {{x} {expr {[string length $x] > 0}}}} \
              ]
查看更多
孤傲高冷的网名
4楼-- · 2019-06-25 13:24

You might also try this:

set listwithoutnulls [lsearch -all -inline -not -exact $listwithnulls {}]

It doesn't require a package include. It, too, can be re-applied.

查看更多
等我变得足够好
5楼-- · 2019-06-25 13:27

Coming from the Tclers Wiki (http://wiki.tcl.tk/440):

To flatten a list:

concat {*}$nested

It can be applied multiple times:

proc flatten data { concat {*}$data }

set a {{a {b c}} {d {e f}}} ; # {a {b c}} {d {e f}} flatten $a ; # a {b c} d {e f} flatten [flatten $a] ; # a b c d e f

查看更多
登录 后发表回答