我想创建一个方便的命令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
OK,一切很好,所以变种。 让我们添加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到其他的名字重新命名的“值”的变量,但它仍然发出“变量数组”的错误。
记:我能在第一PROC添加到合奏:
% namespace ensemble config array -map [list values ::array_values {*}[namespace ensemble config array -map]]
% array values a
bar qux
但是,什么是错的我::tcl::array::values
PROC?
您的set values {}
命令,在TCL :: ::阵列的命名空间执行,所以它运行:: TCL ::阵列:: 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}]
}
我想补充的是,考虑到可能相互冲突的集成命令的存在是一个移动的目标,修补合奏很可能来自世界各地发生,我所看到的核心开发人员保持外额外合奏命令::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中表示,首先)。
对于好奇的,这是我已经结束了:
$ 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