bash - Bash 地图没有给出正确的结果
问题描述
我在 bash 中使用 map 如下:
declare -a hash
hash=(["a"]="A" ["b"]="B" ["c"]="C" ["d"]="D")
echo ${hash["a"]}
但是它正在打印D
而不是A
. 这个片段有什么问题?
解决方案
您的问题就是您如何定义变量哈希。
declare -a hash
用于索引数组,我的意思是索引只是数字的数组。
要声明关联数组,只需将 -a 更改为 -A:
declare -A hash
这样你就可以写字母作为索引,并且回显将按预期工作。
希望我有所帮助!
推荐阅读
- leaflet - 将弹出窗口移出屏幕会意外关闭弹出窗口(带有 leaflet.markercluster 的传单)
- sql - SQL选择发票数据,按日期分组并按最高销售额排序
- css - 带有flavicon和标签的菜单
- crankuptheamps - 在 AMPS 中发布多条记录(crankuptheamps)
- swift - 视图关闭SwiftUI时如何将选择保留在数组中
- reactjs - 如何处理不是TransitionGroup的直接后代的孩子的退出
- jquery - Raja Ongkir api 使用 ajax jquery 获取
- wordpress-gutenberg - 为什么我必须单击古腾堡中以编程方式注入的内容的左侧?
- scala - 数组类型列中元素的数据类型修改
- xamarin.forms - 是否可以让视图对象访问视图模型 Xamarin 表单?