bug-gawk
[Top][All Lists]
Advanced

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

Re: [bug-gawk] Can a key in a array be deleted while the array is iterat


From: Andrew J. Schorr
Subject: Re: [bug-gawk] Can a key in a array be deleted while the array is iterated?
Date: Sun, 1 Jan 2017 22:19:26 -0500
User-agent: Mutt/1.5.23 (2014-03-12)

Hi,

On Sun, Jan 01, 2017 at 07:05:29PM -0600, Peng Yu wrote:
> I made the following function to intersect the keys of x1 and x2. The
> results is saved in x1. I am not sure it is OK to change an array
> while it is being iterated. Will the function always work? Thanks.
> 
> function arrkeyintersect(x1, x2,    k) {
>     for(k in x1) {
>         if(!(k in x2)) {
>             delete x1[k]
>         }
>     }
> }

It is safe. This should be apparent from the documentation:
   https://www.gnu.org/software/gawk/manual/html_node/Delete.html
It is also documented in the Posix awk specification:
   http://pubs.opengroup.org/onlinepubs/9699919799/utilities/awk.html

   The delete statement shall remove an individual array element. Thus, the
   following code deletes an entire array:

   for (index in array)
       delete array[index]

In the future, when you have awk programming questions, please
post them to the comp.lang.awk usenet group. Please use this mailing
list for gawk bug reports only.

Regards,
Andy



reply via email to

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