TCL中列表的行为 [英] Behaviour of list in TCL

查看:36
本文介绍了TCL中列表的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用TCL代码中的一个列表(仅一个元素),当我用puts将其打印出来时,我看到文本有时用大括号括起来.例如:{abc},有时不是.这会导致问题,因为我正在对此特定文本执行regsub,并且花括号使我的代码失败.我还观察到,当文本中有特殊符号时,列表用大括号括起来.谁能解释一下为什么列表用大括号括起来.

I am working with a list (only one element) in my TCL code and when I print it out with puts I see that the text is sometimes enclosed with braces. ex: {abc} and sometimes its not. This causes problems because I am doing a regsub on this particular text and the brace causes my code to fail. I have also observed that the list is enclosed in braces when there are special symbols within the text. Can anyone please explain why the list comes enclosed in braces.

推荐答案

Tcl从单个值创建列表时,它将尝试使用尽可能少的引用.如果它是一个简单的值,例如 abc ,它将忽略所有引号字符.如果它是一个带有诸如空格( abc def )之类的元语法字符的值,则将其括在大括号( {abc def} )中.如果它是( abc {def )中花括号不平衡的值,则使用反斜杠( abc \ {def )).这样可以确保Tcl创建的列表也始终可以理解为包含单个命令的脚本,这些命令的单词恰好是列表的元素.

When Tcl creates a list from individual values, it tries to use as little quoting as possible. If it is a simple value like abc, it omits all quoting characters. If it is a value with a metasyntax character like space in it (abc def), it encloses it in braces ({abc def}). If it is a value with unbalanced braces in (abc{def), it uses backslashes (abc\{def). This ensures that the lists that Tcl creates are always also understandable as scripts containing a single command whose words are exactly the element of the list.

在列表上使用 regexp 并不是一个好主意;格式对于这种工作并不是很友好(而且当您将所有可以解析的内容视为一个列表时,情况甚至更糟,这远远超过了Tcl实际从 list 命令).提取元素(例如,使用 lindex foreach ),然后将正则表达式应用于该元素.

It's not a good idea to use regexp on lists; the format is not really very friendly for that sort of work (and it's even worse when you consider everything that can be parsed as a list, which is a lot more than Tcl will actually ever generate from the list command). Extract the element (e.g., with lindex or foreach) and apply the regular expression to that.

这篇关于TCL中列表的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆