获取结构数组中的索引

时间:2018-01-29 00:50:20

标签: arrays tcl

我有这个结构数组,它位于tcl

A = {1 2 3} {4 5 6} {7 8 9} {1 4 10}

我想获得包含数字4的结构索引,该数字应为2,并且A中为4;我怎么能这样做?

此外,在我能够获得指数后,我想删除这些结构,以便

A = {1 2 3} {7 8 9}

我怎么能这样做?

谢谢!

2 个答案:

答案 0 :(得分:1)

[lmap]可以提供帮助。 [continue]允许您跳过该项:

set A {{1 2 3} {4 5 6} {7 8 9} {1 4 10}}
set B [lmap x $A {
                    if {[lsearch -exact $x 4] >= 0} {
                        continue
                    } else {
                        set x
                    }
                }]
    puts $B

答案 1 :(得分:0)

% array set myArr {
        1 {1 2 3}
        2 {4 5 6}
        3 {7 8 9}
        4 {1 4 10}
}
% parray myArr
myArr(1) = 1 2 3
myArr(2) = 4 5 6
myArr(3) = 7 8 9
myArr(4) = 1 4 10
% set num_to_find 4
4
% foreach idx [array names myArr] {
        # Checking if the array contains the number '4'
        if {[lsearch $myArr($idx) $num_to_find]!=-1} {
                # Keep track of the matched indices
                lappend matchIndices $idx
        }
}
% foreach idx $matchIndices {
        # Removing the array index based on the matched indices
        unset myArr($idx)
}
% parray myArr
myArr(1) = 1 2 3
myArr(3) = 7 8 9
%
%