Tcl 中有获取连续数字数组的简写吗?

发布于 2024-11-28 04:49:02 字数 188 浏览 1 评论 0原文

例如,在 Perl 中,要获取从 1 到 10 的数字的顺序数组,您可以简单地执行以下操作:

@myArray = (1 .. 10);

这两个句点用作此操作的简写,而不是创建 for 循环或手动写出整个内容。我使用过的其他语言也有类似的东西。

Tcl 中是否存在类似的简写?

For example, in Perl, to get a sequential array of numbers from 1 to 10, you could simply do:

@myArray = (1 .. 10);

The two periods serve as shorthand for this operations instead of making a for loop or writing the whole thing out manually. Other languages I've used have something similar also.

Does a similar shorthand exist in Tcl?

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

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

发布评论

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

评论(4

画▽骨i 2024-12-05 04:49:02

您可以定义该方法:

proc fillArray {a b} {
    eval return \[list $a [string repeat "\[incr a\] " [incr b -$a]]\]
}

并将其用作:

set myArray [fillArray 1 10]

您甚至可以美化过程的调用,使其看起来像在 perl 中一样。为此,只需重新定义 unknown 过程:

rename unknown __unknown
proc unknown {args} {
  if {[llength $args] == 3} {
    lassign $args a op b
    if {[string is integer $a] && $op == ".." && [string is integer $b]} {
      return [fillArray $a $b]
    }
  }
  return [uplevel __unknown {*}$args]
}

之后您可以简单地编写为:

set myArray [1 .. 10]

:)

You can define the method:

proc fillArray {a b} {
    eval return \[list $a [string repeat "\[incr a\] " [incr b -$a]]\]
}

And use it as:

set myArray [fillArray 1 10]

You even can beautify the call of procedure to make it look as in perl. For that just redefine unknown procedure:

rename unknown __unknown
proc unknown {args} {
  if {[llength $args] == 3} {
    lassign $args a op b
    if {[string is integer $a] && $op == ".." && [string is integer $b]} {
      return [fillArray $a $b]
    }
  }
  return [uplevel __unknown {*}$args]
}

After that you can write just simple as:

set myArray [1 .. 10]

:)

清旖 2024-12-05 04:49:02

不完全是这个,但

% package require struct::list
1.6.1
% struct::list iota 10
0 1 2 3 4 5 6 7 8 9

也可以在 this 中搜索“iota”关键字,看看如何使用一行。

Not quite this one, but

% package require struct::list
1.6.1
% struct::list iota 10
0 1 2 3 4 5 6 7 8 9

Also search this for the "iota" keyword to see how this can be done using a one-liner.

黯淡〆 2024-12-05 04:49:02

除了表达式(它们自己的小语言)之外,Tcl 没有运算符,并且始终是严格前缀驱动的语言。这意味着没有这样方便的循环简写。另一方面,Tcl 的标准命令没有什么特别之处(除了一些在这里无关紧要的小效率细节),因此制作自己的命令没有问题:

proc .. {from to} {
    if {$from >= $to} {
        for {set i $from} {$i <= $to} {incr i}    {lappend out $i}
    } else {
        for {set i $from} {$i >= $to} {incr i -1} {lappend out $i}
    }
    return $out
}

puts [.. 1 10];   # --> “1 2 3 4 5 6 7 8 9 10”

您可以通过使用未知处理程序来伪造中缀运算符(如 GrAnd 的回答) )但是与上面相比,这确实相当慢。

With the exception of expressions (which are their own little language) Tcl has no operators and is always a strictly prefix-driven language. This means that there isn't such a convenient shorthand for doing loops. On the other hand, there's nothing particularly special about Tcl's standard commands (apart from some minor efficiency details that don't matter here) so making your own is no problem:

proc .. {from to} {
    if {$from >= $to} {
        for {set i $from} {$i <= $to} {incr i}    {lappend out $i}
    } else {
        for {set i $from} {$i >= $to} {incr i -1} {lappend out $i}
    }
    return $out
}

puts [.. 1 10];   # --> “1 2 3 4 5 6 7 8 9 10”

You can fake infix operators by using an unknown handler (as in GrAnd's answer) but that's really quite slow by comparison with the above.

ㄟ。诗瑗 2024-12-05 04:49:02

不,tcl 中不存在类似的简写。

如果您确实想要速记,您可以创建自己的看起来几乎相同的命令。例如:

proc : {start ignore end} {
    set result []
    for {set i $start} {$i <= $end} {incr i} {
        lappend result $i
    }
    return $result
}

puts "from 1 to 10: [: 1 .. 10]"

No, a similar shorthand does not exist in tcl.

If you really want shorthand, you can create your own command that looks almost the same. For example:

proc : {start ignore end} {
    set result []
    for {set i $start} {$i <= $end} {incr i} {
        lappend result $i
    }
    return $result
}

puts "from 1 to 10: [: 1 .. 10]"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文