什么是将关联数组作为参数传递给函数的最佳方法,以避免重复迭代多个关联数组?这样我就可以给函数任何我选择的数组进行打印。以下是我的代码示例:
# 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