相关疑难解决方法(0)

将关联数组作为参数传递给Bash

将关联数组作为参数传递给函数以避免重复必须迭代多个关联数组的最佳方法是什么?这样我可以给函数任意打印的数组.这就是我所拥有的:

# Snippet

declare -A weapons=(
  ['Straight Sword']=75
  ['Tainted Dagger']=54
  ['Imperial Sword']=90
  ['Edged Shuriken']=25
)

print_weapons() {
  for i in "${!weapons[@]}"; do
    printf "%s\t%d\n" "$i" "${weapons[$i]}"
  done
}

print_weapons
Run Code Online (Sandbox Code Playgroud)

linux bash shell

3
推荐指数
2
解决办法
4168
查看次数

标签 统计

bash ×1

linux ×1

shell ×1