我两个数组,说:如何在Bash中获得两个数组的联合?
arr1=("one" "two" "three")
arr2=("two" "four" "six")
会是什么让这两个数组的联合Bash中的最佳方式?
我两个数组,说:如何在Bash中获得两个数组的联合?
arr1=("one" "two" "three")
arr2=("two" "four" "six")
会是什么让这两个数组的联合Bash中的最佳方式?
首先,结合阵列:
arr3=("${arr1[@]}" "${arr2[@]}")
然后,从this后期应用解决方案进行重复数据删除它们:
# Declare an associative array
declare -A arr4
# Store the values of arr3 in arr4 as keys.
for k in "${arr3[@]}"; do arr4["$k"]=1; done
# Extract the keys.
arr5=("${!arr4[@]}")
这是假设的bash 4+。
不需要'arr3':在$ {arr1 [@]}“”$ {arr2 [@]}“中使用' –
之前bash
4,
while read -r; do
arr+=("$REPLY")
done < <(printf '%s\n' "${arr1[@]}" "${arr2[@]}" | sort -u)
sort -u
在其输入端执行免费DUP-结合;循环仅将所有内容都放回到数组中。
实际的联合(没有愚蠢)或简单地两个数组连接? –
实际联盟(没有重复) – user2436428
在zsh中,你可以使用'typeset -U arr',很确定在bash中没有这么简单的方法。 – Kevin