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: Manuel Collado
Subject: Re: [bug-gawk] Can a key in a array be deleted while the array is iterated?
Date: Wed, 04 Jan 2017 15:07:49 +0100
User-agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/17.0 Thunderbird/17.0

El 04/01/2017 12:09, address@hidden escribió:
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 iterating, 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.

I've experimented a bit, and can confirm that additions or deletions during the array traversal are handled differently by different awk implementations. Tested gawk, mawk, nawk and awk95. The first ones always process the original set of indexes. The last ones may omit some deleted items or process also some inserted ones.

Regards.
--
Manuel Collado - http://lml.ls.fi.upm.es/~mcollado




reply via email to

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