在“array"集合中创建新命令的问题 [英] Problem creating new command in `array` ensemble

查看:18
本文介绍了在“array"集合中创建新命令的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个方便的命令 array values arrayName 作为数组名称"命令的另一面".

I want to create a convenience command array values arrayName as a "flip side" to the "array names" command.

创建一个简单的过程很简单:

It's straightforward to create a simple proc:

proc array_values {arrayName} {
    upvar 1 $arrayName ary
    set values {}
    foreach {name value} [array get ary] {lappend values $value}
    return $values
}

array set a {foo bar baz qux}
puts [array_values a]           ;# => bar qux

但是,我在 ::tcl::array 命名空间中创建命令时遇到困难:

However, I'm having difficulty creating a command in the ::tcl::array namespace:

  • 先做一些功课:

  • first some homework:

  1. array 是命名空间集合吗?是的.

  1. is array a namespace ensemble? Yes.

% namespace ensemble exists array
1

  • 什么是命名空间?

  • what is the namespace?

    % namespace ensemble configure array -namespace
    ::tcl::array
    

  • 什么是子命令?

  • what are the subcommands?

    % namespace ensemble configure array -subcommands
    % namespace ensemble configure array -map
    anymore ::tcl::array::anymore donesearch ::tcl::array::donesearch exists ::tcl::array::exists get ::tcl::array::get names ::tcl::array::names nextelement ::tcl::array::nextelement set ::tcl::array::set size ::tcl::array::size startsearch ::tcl::array::startsearch statistics ::tcl::array::statistics unset ::tcl::array::unset
    

  • 好的,一切都很好,所以var.让我们将 array_values proc 添加到命名空间

    OK, all good so var. Let's add that array_values proc into the namespace

    % namespace eval ::tcl::array {
        proc values {arrayName} {
            upvar 1 $arrayName ary
            set values {}
            foreach {name value} [array get ary] {lappend values $value}
            return $values
        }
    }
    % array set a {foo bar baz qux}
    % puts [::tcl::array::values a]
    

    can't set "values": variable is array
    

    这个错误来自哪里?我尝试将 proc 中的values"变量重命名为其他名称,但它仍然发出variable is array"错误.

    Where is this error coming from? I tried renaming the "values" variable in the proc to other names, but it still emits the "variable is array" error.

    注意:我可以将第一个 proc 添加到集合中:

    a note: I can add the first proc to the ensemble:

    % namespace ensemble config array -map [list values ::array_values {*}[namespace ensemble config array -map]]
    % array values a
    bar qux
    

    但是我的 ::tcl::array::values proc 有什么问题?

    But what is wrong with my ::tcl::array::values proc?

    推荐答案

    您的 set values {} 命令在 ::tcl::array 命名空间中执行,因此它运行 ::tcl::数组::设置命令.换句话说,它相当于 array set values {}.所以它使 values 成为一个没有成员的数组.然后 lappend values $value 命令失败,因为此时 values 是一个数组.

    Your set values {} command executes in the ::tcl::array namespace, so it runs the ::tcl::array::set command. In other words, it does the equivalent of array set values {}. So it makes values an array with no members. Then the lappend values $value command fails because values is an array at that point.

    解决方案应该是使用 ::set values {}

    或者您可以使用以下方法完全避免该问题:

    Or you can completely avoid the issue by using:

    proc array_values {arrayName} {
        upvar 1 $arrayName ary
        return [lmap {name value} [get ary] {string cat $value}]
    }
    

    这篇关于在“array"集合中创建新命令的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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