[Top][All Lists]

[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/

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:


declare -A New_Seen
for file in "address@hidden"; do

for file in "address@hidden"; do
  if [[ ${New_Seen["$file"]} ]]; then
     : put code to delete "$file"

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.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]