有什么方法可以在 Tcl 命令中嵌入注释吗?

发布于 2024-08-05 19:24:46 字数 556 浏览 3 评论 0原文

我想在命令中添加注释,但考虑到“#”字符在 Tcl 8.4 中定义为:

如果散列字符(“#”)出现在 Tcl 的位置,则 这似乎是不可能的期望命令第一个单词的第一个字符,然后散列字符及其后面的字符,直到下一个换行符,将被视为注释并被忽略。注释字符仅当出现在命令开头时才有意义。

想象一下这个示例,说明它是如何工作的(这些注释在我的实验中都不起作用):

array set myArray [list red 3        \
                        blue 4       ;# Blue is before purple.
                        purple 5     # Purple is after red.
                        green 7      \
                        yellow 8]

似乎棘手的部分是如何在嵌入注释的情况下继续列表命令?也许类似于 C++ 风格的 /* 嵌入注释。 */ 但我只看到 # 在 Tcl 中用于注释行尾,没有用于开始和结束注释语法。

I would like to have a comment within a command and it appears that this is not possible given that the '#' character is defined in Tcl 8.4 to be:

If a hash character (``#'') appears at a point where Tcl is expecting the first character of the first word of a command, then the hash character and the characters that follow it, up through the next newline, are treated as a comment and ignored. The comment character only has significance when it appears at the beginning of a command.

Imagine this example of how this might work (none of these comments worked in my experiments):

array set myArray [list red 3        \
                        blue 4       ;# Blue is before purple.
                        purple 5     # Purple is after red.
                        green 7      \
                        yellow 8]

Seems the tricky part is how to continue the list command with a comment embedded? Perhaps something like the C++ style of /* Embedded comment here. */ but I only see # as being used in Tcl for comments to end of line, nothing for begin and end comment syntax.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜无邪 2024-08-12 19:24:46

不可以,您不能在命令调用中嵌入注释。 Tcl 中的注释与其他语言中的注释工作方式不太一样。有些人会被这个问题绊倒,大多数有经验的 Tcl 程序员都不会再考虑一下。

在极少数情况下,您确实需要这样做,通常可以很容易地解决它。使用您的示例:

set myArray(red) 3
set myArray(blue) 4 ;# Blue is before purple
set myArray(purple) 5 ;# Purple is after red
set myArray(green) 7
set myArray(yellow) 8

您可能认为这比在一行上完成所有操作要慢,但除了最关键的时间情况外,在所有情况下差异都可以忽略不计,可能只有几微秒。

No, you cannot embed a comment within the invocation of a command. Comments in Tcl don't quite work the same way as they do in other languages. Some people stumble over this, most experienced Tcl programmers don't give it a second thought.

The rare times that you truly need to do this you can usually work around it easily enough. Using your example:

set myArray(red) 3
set myArray(blue) 4 ;# Blue is before purple
set myArray(purple) 5 ;# Purple is after red
set myArray(green) 7
set myArray(yellow) 8

You might think this is slower than doing it all on one line but the difference is negligible in all but the most time-critical situations, probably on the order of just a few microseconds.

美胚控场 2024-08-12 19:24:46

是的,有一种方法可以将注释嵌入到命令中。这并不漂亮,但这是可能的。添加仅包含列表成员注释的命令替换,如下所示(注释后的换行符是强制性的):

array set myArray [list red 3     \
                        blue 4[
                                  # Blue is before purple.
                        ]         \
                        purple 5[
                                  # Purple is after red.
                        ]         \
                        green 7   \
                        yellow 8]
% array get myArray
yellow 8 purple 5 blue 4 green 7 red 3

Yes, there is a way to embed a comment into a command. It's not pretty, but it's possible. Add a command substitution containing only a comment to a list member, like this (the newline after the comment is mandatory):

array set myArray [list red 3     \
                        blue 4[
                                  # Blue is before purple.
                        ]         \
                        purple 5[
                                  # Purple is after red.
                        ]         \
                        green 7   \
                        yellow 8]
% array get myArray
yellow 8 purple 5 blue 4 green 7 red 3
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文