[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Compare 2 arrays.
From: |
Bill Gradwohl |
Subject: |
Re: Compare 2 arrays. |
Date: |
Tue, 29 May 2012 21:25:02 -0600 |
I have no idea what the wget's are supposed to be doing, but here's a
function that will compare 2 foreign arrays and return true 0 or false 1.
compareForeignArrays(){
## $1 and $2 are the names of the arrays to compare.
## These are characters strings.
local intermediary
local sub
intermediary="${1}[@]"
local -a leftValue=("${!intermediary}")
intermediary="${2}[@]"
local -a rightValue=("${!intermediary}")
local -a leftSub
eval leftSub=(\"\${!${1}[@]}\")
local -a rightSub
eval rightSub=(\"\${!${2}[@]}\")
[ ${#leftSub[@]} -ne ${#rightSub[@]} ] || \
[ ${#leftValue[@]} -ne ${#rightValue[@]} ] && return 1
for ((sub=0; sub<${#leftSub[@]}; ++sub)); do
[ "${leftSub[sub]}" -ne "${rightSub[sub]}" ] && return 1
done
for ((sub=0; sub<${#leftValue[@]}; ++sub)); do
[ "${leftValue[sub]}" -ne "${rightValue[sub]}" ] && return 1
done
return 0
}
declare -a this=(1 2 3 4 5)
declare -a that=(1 2 3 4 5)
compareForeignArrays this that && echo Equal || echo NOT Equal
declare -a yours=(10 20 30 40)
declare -a mine=(10 21 30 40)
compareForeignArrays yours mine && echo Equal || echo NOT Equal
declare -a red=(1 2 3 4 5)
declare -a blue=(1 2 3 4)
compareForeignArrays red blue && echo Equal || echo NOT Equal
declare -a night=(1 2 [7]=3 4 5)
declare -a day=(1 2 [6]=3 4 5)
compareForeignArrays night day && echo Equal || echo NOT Equal
--
Bill Gradwohl