[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Compare 2 arrays.
From: |
Greg Wooledge |
Subject: |
Re: Compare 2 arrays. |
Date: |
Wed, 30 May 2012 08:33:49 -0400 |
User-agent: |
Mutt/1.4.2.3i |
On Tue, May 29, 2012 at 09:25:02PM -0600, Bill Gradwohl wrote:
> 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}")
Hacks like this are precisely why I stress that such functions *should
not* be written in bash. If you want to compare two arrays, use a loop,
without wrapping a function around it. That way you have access to the
arrays directly, by their actual names, in the actual context where they
are defined.
The only Bourne-family shell that can manipulate arrays whose names are
passed to a function is ksh93, with its "nameref" command. Bash has
nothing analogous to that yet.
I don't know whether the tmp="$1[@]"; x=("${!tmp}") trick is a bug or
a feature. It's certainly undocumented. I don't know whether it will
go away in some future version of bash, with or without a notification in
the changelog, since it looks like a bug that people are exploiting.