TCL错误:无法设置" :: streamID(1,10,1)":变量不是数组

时间:2017-12-09 00:13:16

标签: arrays tcl incr-tcl

我已阅读帖子Cant read variable, isnt array,我认为可能会以某种方式相关,但我无法弄清楚如何。在以下TCL片段中,测试三维数组::流并读取值。该数组包含标量ID值。代码段中的最后一行会产生一个错误,表示

can't set "::streamId(1,10,1)": variable isn't array
    while executing
"set ::streamId($chas,$card,$port) $nextId"
    (procedure "getNextStreamId" line 28)

我将此解释为含义$ nextId不是标量,它不能放入标量的三维数组中。我对错误的解释是不正确的?我非常确信该数组具有标量值,因此我开始认为这里可能存在一些数据安全问题。

# get current streamId
if { [ catch {info exists $::streamId($chas,$card,$port)} ] == 0 } {
if {$::testEnv(verbose) >= $verbLevel} {
    logInfo [getProcName] "pre-existing streamId found for: \
        \n dutAndPort:  $dutAndPort \
        \n ixiaPort:    {$chas $card $port}\
        \n streamId:    $::streamId($chas,$card,$port) \
        \n incrementing to next one..."
}
set nextId [ mpexpr $::streamId($chas,$card,$port) + 1 ]
} else {
if {$::testEnv(verbose) >= 0} {
    logInfo [getProcName] "No pre-existing streamId found for: \
        \n\t dutAndPort:    $dutAndPort \
        \n\t ixiaPort:      {$chas $card $port}\
        \n\t setting to 1"
}
set nextId 1
}
set curId  [ mpexpr $nextId - 1 ]

set ::streamId($chas,$card,$port) $nextId

1 个答案:

答案 0 :(得分:2)

在您的代码中,我想您想检查数组::streamId是否具有索引$chas,$card,$port

info exists $::streamId($chas,$card,$port)

这是不正确的。你应该使用

info exists ::streamId($chas,$card,$port)

即。没有美元符号。然后只有if循环可以确保索引$chas,$card,$port的存在。

然后,您最后尝试将索引$chas,$card,$port的值设置为$nextId

set ::streamId($chas,$card,$port) $nextId

这是不完整的,因为它保留在索引$chas,$card,$port的变量存在检查的if循环之外。

然后实际的错误消息是指存在名为streamId的标量变量的事实。

% set ::streamId 1
1
% set ::streamId(1,10,1) 20
can't set "::streamId(1,10,1)": variable isn't array
%

确保您没有相同的变量名称。