Mic*_*aël 5 zsh associative-array
我拥有的关联数组具有任意键,包括包含反引号、方括号等的键:
$ typeset -A arr
$ key='`'
$ arr[$key]=backquote
$ echo $arr[$key]
backquote
Run Code Online (Sandbox Code Playgroud)
我现在需要取消设置arr[$key]
。通过网络查看,我尝试了:
$ unset "arr[$key]"
unset: arr[`]: invalid parameter name
$ unset "arr[${(b)key}]"
unset: arr[`]: invalid parameter name
Run Code Online (Sandbox Code Playgroud)
……没有运气。现在,我有点幸运,因为这提供了一条错误消息;在以下情况下,除了结果之外似乎没有任何失败:
$ typeset -A arr
$ key='?~>#'
$ arr[$key]=symbols
$ echo "$arr[$key]"
symbols
$ unset "arr[${(b)key}]"
$ echo "$arr[$key]"
symbols
Run Code Online (Sandbox Code Playgroud)
事实上, 中的任何符号?~>#
都会触发相同的行为。
关于正在发生的事情以及如何获得预期行为的任何澄清?
哇,这是一团糟。
由于Bart Schaefer 的 2016 补丁,在提交 95663e936596933d529a648ed3d6c707d1a1dffe 中合并为补丁 37914并首次在 zsh 5.4 中发布,实验性地,unset "arr[$key]"
只要key
不包含以下任何字符即可工作:\`()[]
. 这六个字符必须以反斜杠作为前缀,而其他字符则不能(例如unset 'arr[\*] arr[\;]'
尝试取消设置键\*
and \;
、 not*
和;
)。这不是任何引用参数扩展标志(${(b)key}
,${(q)key}
及其变体)所做的。
此外,还有一个问题:我找不到取消设置空键的方法。unset 'arr[]'
是一个错误,其他任何东西都会取消设置非空键。我发现取消设置空键的唯一解决方法是完全重新分配数组,使用下标标志过滤掉不需要的键(如 Stéphane Chazelas 在2018 zsh-workers thread 中建议的那样)。
以下函数适用于 zsh ?5.4(在 zsh 5.8 中测试),并且仅在需要删除空键时才复制数组。
# Usage: unset_keys ARRAY [KEY]...
# ARRAY must be the name of an associative array parameter.
# Equivalent to unset 'ARRAY[KEY1]' 'ARRAY[KEY2]' ...
# except that this function works correctly even with keys containing
# special characters or is empty. See
# https://unix.stackexchange.com/questions/626393/in-zsh-how-do-i-unset-an-arbitrary-associative-array-element
function unset_keys {
emulate -LR zsh
if [[ -${(Pt)1}- != *-association-* ]]; then
return 120 # Fail early if $1 is not the name of an associative array
fi
if ((${#@[2,$#]:#?*})); then
if [[ -n ${${(P)1}[${:-}]+y} ]]; then
# Copy all entries with non-empty keys
: "${(AAP)1::=${(@kv)${(P)1}[(I)?*]}}"
fi
set -- $@ # Remove empty keys from the to-do list
fi
if (($# < 2)); then
return 0
fi
set -- "$1" "${@[2,$#]//\\/\\\\}"
set -- "$1" "${@[2,$#]//\`/\\\`}"
set -- "$1" "${@[2,$#]//\(/\\(}"
set -- "$1" "${@[2,$#]//\)/\\)}"
set -- "$1" "${@[2,$#]//\[/\\[}"
set -- "$1" "${@[2,$#]//\]/\\]}"
noglob unset $1[${^@[2,$#]}]
}
Run Code Online (Sandbox Code Playgroud)
这是一个更简单的函数,无论如何都只做一个副本。
# Usage: unset_keys ARRAY [KEY]...
# ARRAY must be the name of an associative array parameter.
# Equivalent to unset 'ARRAY[KEY1]' 'ARRAY[KEY2]' ...
# except that this function works correctly even with keys containing
# special characters or is empty. See
# https://unix.stackexchange.com/questions/626393/in-zsh-how-do-i-unset-an-arbitrary-associative-array-element
function unset_keys {
emulate -LR zsh
setopt extended_glob
if [[ -${(Pt)1}- != *-association-* ]]; then
return 120 # Fail early if $1 is not the name of an associative array
fi
set -- "$1" "${(j:|:)${(@b)@[2,$#]}}"
# Copy all entries except the specified ones
: "${(AAP)1::=${(@kv)${(P)1}[(I)^($~2)]}}"
}
Run Code Online (Sandbox Code Playgroud)
在 zsh 5.4 之前,这是一个我还没有探索过的混乱局面。
这是我使用的测试工具。我认为它提供了合理的覆盖范围,但我没有花任何时间来完善它。
set -e
test_keys=(
'()safe' '(r)set' '(R)set' '(k)safe' # look like valid subscript flags
'(a' '(a)' '(n:foo:)a' '(n:1)a' # look like invalid subscript flags
'set' '"set"' \'set\' '\s\e\t'
'safe' '"safe"' \'safe\' '\s\a\f\e'
'\\' '\\\' '\\\\' '""' \'\'
'two words' 'two spaces' ' initial space' 'trailing space '
$'\x80\\' $'\x80\`' $'\x80\~' # broken UTF-8
''
'?~>#'
)
for ((i=0; i<255; i++)); do
printf -v n '\\x%02x' $i
eval "test_keys+=(\$'$n')"
done
function populate_test_array {
for k in "${(@)test_keys}"; do
arr[$k]=set
done
}
function check_expected_keys {
local -a actual_keys
actual_keys=("${(@k)arr}")
actual_keys=("${(@o)actual_keys}") # Sorting in one step seems to misplace the empty string at the end (zsh 5.8 on Ubuntu 20.04), so sort in two steps.
local actual_list="${(j: :)${(@qqqq)actual_keys}}"
local expected_list="${(j: :)${(@qqqq)expected_keys}}"
if [[ "$actual_list" != "$expected_list" ]]; then
<<EOF
Failure: unexpected list of keys after $1
expected: $expected_list
actual : $actual_list
EOF
((++errors))
fi
}
typeset -A arr
errors=0
populate_test_array
expected_keys=("${(@o)test_keys}")
test_keys=("${(@)test_keys:#safe}") # [safe] must stay until the end
for k in "${(@)test_keys}"; do
unset_keys arr "$k"
if (($+arr[$k])); then
printf 'Failure: unset %s did not unset it\n' "${(qq)k}"
((++errors))
else
expected_keys=("${(@)expected_keys:#"$k"}")
fi
check_expected_keys "unset ${(qq)k}"
done
populate_test_array
unset_keys arr "${(@)test_keys}"
expected_keys=(safe)
check_expected_keys "unsetting all"
exit $((!!errors))
Run Code Online (Sandbox Code Playgroud)