Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List Tcl lappend返回意外结果_List_Tcl_Embedded Linux_Tclsh - Fatal编程技术网

List Tcl lappend返回意外结果

List Tcl lappend返回意外结果,list,tcl,embedded-linux,tclsh,List,Tcl,Embedded Linux,Tclsh,我正在处理,lappend操作符返回了意外的结果 我在F5负载平衡硬件的命令行界面上运行这个。以下是相关的系统信息: ~ \# cat /proc/version Linux version 2.6.32-431.56.1.el6.f5.x86_64 (f5cm@build19) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-3) (GCC) ) #1 SMP Wed Jun 8 11:41:48 PDT 2016 % puts $tcl_vers

我正在处理,lappend操作符返回了意外的结果

我在F5负载平衡硬件的命令行界面上运行这个。以下是相关的系统信息:

~ \#  cat /proc/version
  Linux version 2.6.32-431.56.1.el6.f5.x86_64 (f5cm@build19) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-3) (GCC) ) #1 SMP Wed Jun 8 11:41:48 PDT 2016

% puts $tcl_version
8.5
我尝试了我能想到的所有变量分组的排列,但仍然无法得到我期望的结果。看起来好像有一个缓冲区保存着命令的所有结果:“puts”命令并在“lappend”命令中使用它。这是我执行的行。前几个“put”只是为了表明尚未初始化任何内容:

% puts $l1

can't read "l1": no such variable

% puts $l2

can't read "l2": no such variable

% puts $l3

can't read "l3": no such variable

% puts $l4

can't read "l4": no such variable

% puts $l5

can't read "l5": no such variable

% set l1 { {item 1} {item 2} {item 3} }

 {item 1} {item 2} {item 3}

% set l2 { {item 4} {item 5} {item 6} }

 {item 4} {item 5} {item 6}

% set l3 [concat $l1 $l2]

{item 1} {item 2} {item 3} {item 4} {item 5} {item 6}

#things working as expected here
% puts $l3

{item 1} {item 2} {item 3} {item 4} {item 5} {item 6}

#this is where things start to get squirrelly. I would expect this to return the result of $l1 concat with $l2 and the result stored in $l1
% lappend $l1 $l2

{ {item 4} {item 5} {item 6} }

#as you can see, it appears to return the second argument when that argument is a list. 
% lappend $l2 $l1

{ {item 1} {item 2} {item 3} }

# $l1 remains unchanged. at the very least, according to the documentation,
# I would expect that second item would be treated as a single entity
# when it is a list, and that the fourth item in '% lappend $l2 $l1' would be $l1
% puts $l1

 {item 1} {item 2} {item 3}

#neither $l2 nor $l1 are modified as the result of the 'lappend' command.
% puts $l2

 {item 4} {item 5} {item 6}

#more squirrelly-ness. when the arguments being passed are individual, it seems as though the last call to 'puts' is what 'lappend' uses for its first argument. this is confirmed on the last 3 commands below. **strong text**
% lappend $l1 "a" "b" "c"

{ {item 4} {item 5} {item 6} } a b c

% puts $l1

 {item 1} {item 2} {item 3}

% lappend "$l1" "$l2"

**{ {item 4} {item 5} {item 6} } a b c { {item 4} {item 5} {item 6} }**

% puts $l1

 {item 1} {item 2} {item 3}

% puts $l2

 {item 4} {item 5} {item 6}

% set l4 [lappend $l1 $l2]

 **{ {item 4} {item 5} {item 6} } a b c { {item 4} {item 5} {item 6} } { {item 4} 
{item 5} {item 6} }**

% puts $l4

 { {item 4} {item 5} {item 6} } a b c { {item 4} {item 5} {item 6} } { {item 4} 
{item 5} {item 6} }

# confirmed. 'lappend' is using last call to 'puts' as its argument for it's first argument. this can't be intended behavior right?
% puts $l1
 {item 1} {item 2} {item 3}
% set l5 [lappend $l2 "a" "b" "c"]
 { {item 1} {item 2} {item 3} } a b c
% puts $l2
 {item 4} {item 5} {item 6}
我无法想象这种行为是故意的

以下是我认为这应该如何工作:

#should return something like [$list1, [$list2]] or something like concat $list1 $list2
% lappend $list1 $list2

#should return each item concatenated to the end of $list1
% lappend $list1 "a" "b" "c"
如果答案是lappend没有适当地修改第一个参数,并且我必须使用set命令来保存lappend命令的结果,那就可以了;但是,lappend命令的行为似乎并不一致


提前感谢您提供的任何帮助/见解

lappend$l1$l2
将l2的内容附加到由l1的内容命名的变量。您需要
lappend$l1$l2
,这与您通过
set l1 whatever
设置变量的方式大致相同,而不是
set$l1 whatever

lappend$l1$l2
将l2的内容附加到由l1的内容命名的变量。您需要
lappend l1$l2
,这与您通过
set l1 whatever
设置变量的方式大致相同,而不是
set$l1 whatever

lappend
将变量名称作为其第一个参数,而不是列表。 在这种情况下:

   set l1 [list a b c]
   lappend $l1 x
   puts [set {a b c}]
   # returns: x
x
附加到名为
{abc}
的变量

相反,使用变量名作为lappend的第一个参数:

   set l1 [list a b c]
   set l2 [list d e f]
   lappend l1 {*}$l2
   # result: a b c d e f
要记住的一般规则是,如果Tcl命令修改其参数,则传递一个变量名。如果Tcl命令不修改其参数,则传递该值(此规则不适用于数组)


引用:

lappend
将变量名作为其第一个参数,而不是列表。 在这种情况下:

   set l1 [list a b c]
   lappend $l1 x
   puts [set {a b c}]
   # returns: x
x
附加到名为
{abc}
的变量

相反,使用变量名作为lappend的第一个参数:

   set l1 [list a b c]
   set l2 [list d e f]
   lappend l1 {*}$l2
   # result: a b c d e f
要记住的一般规则是,如果Tcl命令修改其参数,则传递一个变量名。如果Tcl命令不修改其参数,则传递该值(此规则不适用于数组)


参考文献:

Ahh。明白了,谢谢。这有点像C型指针。我想要列表本身,而不是列表的内容?@JoeGuichebarou因为您正在修改它,所以您想要保存列表的变量,而不是变量中的列表值。啊。明白了,谢谢。这有点像C型指针。我想要的是列表本身,而不是列表的内容?@JoeGuichebarou,因为您正在修改它,您想要的是保存列表的变量,而不是变量中的列表值。不幸的是,该教程在语法规范中没有做出适当的区分。我认为目前,您还需要调出手册页面,以便查看正确的语法。还有其他的教程。不幸的是,该教程在语法规范上没有做出适当的区分。我认为目前,您还需要调出手册页面,以便查看正确的语法。还有其他教程可用。您在此处调用未定义的行为。当您运行
lappend$l1$l2
时,
lappend
需要一个可写变量作为其第一个参数,但您给它一个只读变量。如果它是
lappend l1$l2
(注意
l1
中没有
$
),它将按预期工作。
concat$l1$l2
的等价物是
lappend'$l1$l2
。谢谢这是一个关于空字符串的巧妙小把戏。这不是空字符串。这是一个名为
'
的变量。Tcl不使用单引号引用任何内容。这是因为“{item 1}{item 2}{item 3}”(带空格)在Tcl中是一个有效(但非常罕见)的变量名。根据文档,括在方括号中的所有内容都被视为文本。您在此处调用未定义的行为。当您运行
lappend$l1$l2
时,
lappend
需要一个可写变量作为其第一个参数,但您给它一个只读变量。如果它是
lappend l1$l2
(注意
l1
中没有
$
),它将按预期工作。
concat$l1$l2
的等价物是
lappend'$l1$l2
。谢谢这是一个关于空字符串的巧妙小把戏。这不是空字符串。这是一个名为
'
的变量。Tcl不使用单引号来引用任何内容。这是因为“{item 1}{item 2}{item 3}”(带空格)在Tcl中是一个有效(但非常不寻常)的变量名。根据文档,括在花括号中的所有内容都被视为文本。