我想创建一个便捷命令array values arrayName
作为“数组名称”命令的“反面”。
创建一个简单的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
名称空间中创建命令:
先做一些家庭作业:
array
是命名空间集合吗?是的。
% namespace ensemble exists array
1
什么是名称空间?
% namespace ensemble configure array -namespace
::tcl::array
什么是子命令?
% 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
好的,一切都很好。让我们将array_values
proc添加到名称空间
% 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”错误。
注意:我可以将第一个proc添加到合奏:
% namespace ensemble config array -map [list values ::array_values {*}[namespace ensemble config array -map]]
% array values a
bar qux
但是我的::tcl::array::values
处理程序出了什么问题?
答案 0 :(得分:3)
您的set values {}
命令在:: tcl :: array命名空间中执行,因此它运行:: tcl :: array :: set命令。换句话说,它等效于array set values {}
。因此,它使值成为没有成员的数组。然后lappend values $value
命令失败,因为值在那时是一个数组。
解决方案应该是使用::set values {}
或者您可以使用以下方法完全避免该问题:
proc array_values {arrayName} {
upvar 1 $arrayName ary
return [lmap {name value} [get ary] {string cat $value}]
}
答案 1 :(得分:1)
我想补充一点,鉴于可能存在冲突的集成命令是一个移动目标,因此修补集成可能在任何地方发生,我已经看到核心开发人员在::tcl::array::*
之外保留了额外的集成命令。命名空间:
proc arrayValues {arrayName} {
upvar 1 $arrayName ary
set values {}
foreach {name value} [array get ary] {lappend values $value}
return $values
}
# implant "arrayValues" into [array] ensemble as "values"
namespace ensemble configure ::array -map \
[dict replace [namespace ensemble configure ::array -map] \
values [namespace which arrayValues]]
通过这种方式,您不必担心意外的分辨率冲突(无论从Tcl开始,这意味着什么)。
答案 2 :(得分:0)
出于好奇,这就是我最后得到的:
$ HOME / tcl / lib / monkeypatches / monkeypatches.tcl
# a set of useful additions to built-in ensembles
package provide monkeypatches 0.1
namespace eval ::monkeypatches {
# https://wiki.tcl-lang.org/page/wrapping+commands
proc append_subcommand {cmd subcmd procname} {
set map [namespace ensemble configure ::$cmd -map]
dict set map $subcmd [namespace which $procname]
namespace ensemble configure ::$cmd -map $map
}
# array foreach
# to be subsumed by https://core.tcl.tk/tips/doc/trunk/tip/421.md
#
# example:
# array set A {foo bar baz qux}
# array foreach {key val} A {puts "name=$key, value=$val"}
#
proc array_foreach {vars arrayName body} {
if {[llength $vars] != 2} {
error {array foreach: "vars" must be a 2 element list}
}
lassign $vars keyVar valueVar
# Using the complicated `upvar 1 $arrayName $arrayName` so that any
# error messages propagate up with the user's array name
upvar 1 $arrayName $arrayName \
$keyVar key \
$valueVar value
set sid [array startsearch $arrayName]
# If the array is modified while a search is ongoing, the searchID will
# be invalidated: wrap the commands that use $sid in a try block.
try {
while {[array anymore $arrayName $sid]} {
set key [array nextelement $arrayName $sid]
set value [set "${arrayName}($key)"]
uplevel 1 $body
}
array donesearch $arrayName $sid
} trap {TCL LOOKUP ARRAYSEARCH} {"" e} {
return -options $e "detected attempt to modify the array while iterating"
}
return
}
append_subcommand array foreach array_foreach
# array values arrayName
# https://stackoverflow.com/q/53379995/7552
#
# example:
# array set x {foo bar baz qux}
# array get x ;# => foo bar baz qux
# array names x ;# => foo baz
# array values x ;# => bar qux
#
proc array_values {arrayName} {
upvar 1 $arrayName ary
set values [list]
array foreach {name value} ary {lappend values $value}
return $values
}
append_subcommand array values array_values
# info formalargs procName
# https://core.tcl.tk/tips/doc/trunk/tip/65.md
#
# example:
# proc test {one {two 2} {three {3 4 5}} args} {return}
# info args test ;# => one two three args
# info formalargs test ;# => one {two 2} {three {3 4 5}} args
#
proc info_formalargs {procname} {
# [info args] throws an error if $procname is not a procedure.
return [lmap arg [info args $procname] {
set has_d [info default $procname $arg value]
if {$has_d} then {list $arg $value} else {set arg}
}]
}
append_subcommand info formalargs info_formalargs
}
及其关联的pkgIndex.tcl
和$ HOME / .tclshrc
set lib_dir [file join $env(HOME) tcl lib]
if {$lib_dir ni $auto_path} {lappend auto_path $lib_dir}
unset lib_dir
package require monkeypatches