gpt4 book ai didi

zsh - 通过通配符过滤 zsh 数组

转载 作者:行者123 更新时间:2023-12-05 00:52:59 25 4
gpt4 key购买 nike

给定一个 Zsh 数组 myarray,我可以把它作为一个子集数组

set -A subarray
for el in $myarray
do
if [[ $el =~ *x*y* ]]
then
subarray+=($el)
fi
done

在本例中,它包含 myarray 中的所有元素,这些元素按顺序在某处具有 x 和 y。

题:

鉴于 zsh 中有大量可用的数组操作,是否有更简单的方法来实现这一点?我检查了手册页和 zsh-lovers page ,但找不到合适的。

最佳答案

这应该可以解决问题

subarray=(${(M)myarray:#*x*y*z})
您可以在 zsh 联机帮助页的 [关于参数扩展的部分] 中找到解释。它有点隐藏为 ${name:#pattern}无旗 (M)与你想要的相反:

${name:#pattern}

If the pattern matches the value of name, then substitute the empty string; otherwise, just substitute the value of name. If name is an array the matching array elements are removed (use the (M) flag to remove the non-matched elements).

关于zsh - 通过通配符过滤 zsh 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41872135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com