【问题标题】:tcl/itcl - foreach iteration on args input list with two argumentstcl/itcl - 带有两个参数的 args 输入列表上的 foreach 迭代
【发布时间】:2018-08-02 11:47:35
【问题描述】:

我编写了以下代码,应该在我的 args 输入列表上进行迭代,每次迭代都读取两个参数。问题是它不像我写的那样工作。我查看了 Tcl/Tk Wikipedia 网页,但找不到任何有用的建议。我可以按照我写的方式(不将其转换为数组)吗?

itcl::body class::config {args} {
if {[llength $args] > 1} {
    foreach {option value} in $args {
        if {[string length $option] == 0 || [string length $value] == 0} {
            puts "Runtime error::Bad Input: option flag or value is missing"
            return
        }
        switch --$option {
            -a { 
                if { [string is integer $value -strict] } {
                    #do something
                }
            }
            -b { 
                    if { [string is integer $value -strict] } {
                    #do something
                }
            }
        }
return }

【问题讨论】:

标签: tcl itcl


【解决方案1】:

删除in,你只需要:

foreach {option value} $args {

请参阅https://www.tcl.tk/man/tcl/TclCmd/foreach.htm 的文档

【讨论】:

    【解决方案2】:

    一些提示。 而不是:

    [string length $option] == 0
    

    你也可以写:

    $option == ""
    

    在这种情况下,-strict 选项是多余的,因为您已经跳过了空字符串。此外,您应该将选项放在“是整数”之后:

    string is integer -strict $value
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-01
      • 1970-01-01
      • 2021-01-16
      • 2019-03-11
      • 2018-10-23
      • 1970-01-01
      相关资源
      最近更新 更多