tcl中的多维数组搜索

时间:2018-07-20 04:42:24

标签: tcl

我在tcl中使用多维关联数组,从第四列到N列我正在存储值,并且我将每个列的值与第二列进行比较并将结果存储在第三列中,有时它会完美地搜索有时它会跳过这些值并给出错误。是否有更好的方法在tcl中搜索多维数组?这段代码有什么问题?

for {set index 1} {$index < $count} {incr index} {
    for {set val 3} {$val < $d1 } {incr val} {
        if {$flag1 != 1} {
            if {$asarr($index,2)== $asarr($index,$val)} {
                set asarr($index,1) 50 
            } else {
                set asarr($index,1) 100
                set flag1 1
                break  
            }
        }
    }
    set flag1 0
}

1 个答案:

答案 0 :(得分:0)

您的代码与问题中的问题说明之间存在差异。

根据您的问题,我总结了这些要求:

  1. 数据存储在二维数组中
  2. 每行存储一组数据
  3. 在每一行中,数据存储在以下列中:
    • 原始问题中未提及第一栏
    • 第二列包含一个将与所有“数据列”进行比较的值
    • 第3列是比较结果的存储位置(查看代码,结果为50或100)
    • 第4列至第N列保存需要与第2列的值进行比较的数据

这是您的代码实际执行的操作:

Symfony\Component\Validator\Validator\ValidatorInterface

如果我们假设在FOR循环开始之前将'flag1'设置为0(或任何!= 1),那么最终结果是代码遍历数组行,比较每列(从第3个开始!)针对第2列,它仅对所有其他列等于第2列并且每次时间在第1列上覆盖相同的值“ 50”时才重复此操作,一旦遇到不同的值,则结果为“ 100”写入第1列,其余值立即被跳过。然后从头开始执行,但从下一行开始。

这与您的初衷相比有何不同? 可以(并且应该)简化代码,至少不会多次重写相同的值,但是,我不确定您要实现的目标。请尝试阐明您的要求。

我不知道这是否有用(您说过代码不会像您希望的那样运行),但是这是一个替代版本,它根据我从问题中猜到的要求(并列出了)运行上方):

# start iterating array by rows
# first row has index 1    

for {set index 1} {$index < $count} {incr index} {

    # start iterating over columns
    # NOTE: either column indexes are assumed to start at 0
    #       or we, in fact, start with the 3rd column instead of 4th

    for {set val 3} {$val < $d1 } {incr val} {

            # check the special flag named 'flag1'
            # NOTE: it would be better to give it a meaningful name
            # such as 'found' since it signals that we found
            # an element equal to the one in 2nd column

            if {$flag1 != 1} {

                if {$asarr($index,2)== $asarr($index,$val)} {

                    # if the element at current position ($index,$val)
                    # is equal to the element at ($index,2)
                    # write '50' at ($index,1)
                    # NOTE: result is stored in 1st column,
                    #        not in 3rd as the question would assume

                    set asarr($index,1) 50 

                } else {

                    # if the current element is not equal to the one
                    # in 2nd column, write '100' at ($index, 1).
                    # Again, this is not the 3rd column as requested originally

                    set asarr($index,1) 100

                    # set the special flag to 1, meaning that the above
                    # IF clause will not be run again
                    set flag1 1

                    # execute a break, meaning that we immediately interrupt
                    # the inner FOR loop, meaning that the above IF clause
                    # will not be run again in this iteration.
                    # As Donal pointed out, you could completely
                    # remove the $flag1 and achieve the same result with
                    # this break clause
                    break  
                }
            }
        }
        # reset the flag so that IF clause is executed at least once
        # for the next iteration of the outer loop
        set flag1 0
    }