将数组的每个元素乘以 zsh 中的相同标量
我想将 zsh 中数组中的每个元素乘以相同的标量。我可以在 Python 中非常轻松地完成此操作,但查看了很多地方,但无法弄清楚如何在 zsh 脚本中完成此操作。
我想编写类似的代码
#!/bin/zsh
mean=(1 2 2 1 -1 -2 -2 -1)
echo $((2 * $mean))
,但是当我尝试这样做时,我收到错误消息
bad math expression: operator expected at `2 2 1 -1 -...'
任何和所有帮助将不胜感激。我似乎无法在网上找到任何与我想要弄清楚如何做的事情相近的东西。
I would like to multiply every element in an array in zsh by the same scalar. I would be able to do this very easily in Python, but have looked in many places and cannot figure out how to do it in a zsh script.
I would like to write code like
#!/bin/zsh
mean=(1 2 2 1 -1 -2 -2 -1)
echo $((2 * $mean))
But when I try to do this I get the error message
bad math expression: operator expected at `2 2 1 -1 -...'
Any and all help would be appreciated. I can't seem to find anything online that is even remotely close to what I'm trying to figure out how to do.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须迭代数组的元素并将每个元素相乘:
You have to iterate over the elements of the array and multiply each one:
有一些奇怪的方法来实现
map()
:但循环可能是最简单的:
There are some hacky ways to implement a
map()
:But a loop is probably the easiest: