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: arnold
Subject: Re: [bug-gawk] Can a key in a array be deleted while the array is iterated?
Date: Wed, 04 Jan 2017 04:09:33 -0700
User-agent: Heirloom mailx 12.4 7/29/08

Manuel Collado <address@hidden> wrote:
> My point is about an hypothetical code that iterates over an array and 
> the iteration body modifies an element other that the current one. I.e.:
>
> for (k in arr) {
>      ... modify arr[j] ... # j != k
> }
>
> In that case the behavior may depend on the index iteration order. The 
> modified arr[j] may or may not be already iterated over.

Yes, this is undefined territory.  A stronger question is what happens if
you insert new elements while iterting, will they be included or not?

Quite some time ago I fixed gawk so that before starting to iterate
it makes a list of what's in the array and only traverses that list;
elements added during the loop won't be included.  But I'm not sure that
other awks can make that guarantee, and I doubt if POSIX requires this
behavior.

Arnold



reply via email to

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