[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Comparing arrays
From: |
Greg Wooledge |
Subject: |
Re: [Help-bash] Comparing arrays |
Date: |
Mon, 1 Apr 2013 08:17:12 -0400 |
User-agent: |
Mutt/1.4.2.3i |
On Sun, Mar 31, 2013 at 03:34:37PM -0400, Jerry wrote:
> Using Bash-4.x, I have created two arrays.
>
> declare -a New_Files # Contains a list of files
> declare -a Old_Files # Contents of a directory
>
> Now, what I am attempting to do is check the list of existing files
> "Old_Files" against the list of "New_Files". If any of the files in
> "Old_Files" does not exist in the "New_Files" list, it is to be
> deleted.
The most efficient approach is to use an associative array:
New_Files=(...)
Old_Files=(...)
declare -A New_Seen
for file in "address@hidden"; do
New_Seen["$file"]=1
done
for file in "address@hidden"; do
if [[ ${New_Seen["$file"]} ]]; then
: put code to delete "$file"
fi
done
If the associative array implementation is optimally efficient, the
lookup of each file in the New_Seen AA should be done in O(1) instead
of O(n) time. I'm not sure how efficient Bash's implementation
actually is. Nevertheless, this is the algorithm of choice.
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- Re: [Help-bash] Comparing arrays,
Greg Wooledge <=