bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: delete elements from an array...


From: Greg Wooledge
Subject: Re: delete elements from an array...
Date: Wed, 11 Mar 2009 09:27:42 -0400
User-agent: Mutt/1.4.2.2i

On Tue, Mar 10, 2009 at 03:21:38PM -0400, Chris F.A. Johnson wrote:
> while [ $n -lt ${#array2[@]} ]
> do
>   case ${array2[$n]} in
>     *"$match"*)
>          array1[${#array1[@]}]=${array2[$n]}
>          unset array2[n]

Unsetting elements of array2 will create holes in the array, which means
${#array2[@]} will no longer be a useful way to iterate through the
whole array.  (${#array2[@]} will become too small, as it counts the
number of elements, rather than naming the highest index and adding one.)

To iterate over a sparse array by index, you need to use ${!array2[@]}
(requires bash 3.0 or higher) which returns the list of indices.  Try
replacing your while loop with:

  for n in ${!array2[@]}




reply via email to

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