[Top][All Lists]

[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 10:57:59 +0100
User-agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/17.0 Thunderbird/17.0

El 03/01/2017 17:37, Andrew J. Schorr escribió:
On Tue, Jan 03, 2017 at 03:56:16PM +0100, Manuel Collado wrote:
Well, it seems safe to modify the current iterated array element. But
modifying array elements other then the current one may be troublesome.


It's not clear to me. Can you please elaborate on why this code is dangerous?

Well, neither the original code nor the modified one are dangerous. While iterating over the array, the original one modifies only the current element, and the modified code does not modify any element of the iterated array.

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.

For instance, what should be the result of the following code?

-------- arrayiter.awk
    for (k=0; k<10; k++) arr[k] = k
    for (k in arr) arr[(k+1) % 10] = arr[k]  # copies into the next
    for (k in arr) print k, arr[k]

It depends on the iteration order:

I:\pruebas\awk>gawk -f arrayiter.awk
0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0

I:\pruebas\awk>mawk -f arrayiter.awk
3 2
8 6
6 5
5 3
2 1
1 0
4 3
0 8
9 8
7 6

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

reply via email to

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