[Top][All Lists]

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

Bug on array manipulation?

From: Joao Batista Souza de Oliveira
Subject: Bug on array manipulation?
Date: Wed, 20 Dec 2006 09:27:21 -0200


I have found a rather strange behavior in gawk. It might be a bug, it
might as well be arcane behavior due to access to arrays, but I do not
have the expertise to judge. So I decided to report it.

The code example attached shows the problem.

First we make N = ARGV[1] to get an integer number.

A vector Cl with N elements is initialized with elements from 1 to N.
Thus, in the beginning all elements in Cl are different.

The unify() function does the following:

  - it chooses two values mi and mj from the vector Cl, assuring that
    they are different.

  - Next, it examines all elements in Cl and changes occurrences of mj
    to mi. By doing so we are creating repeated values in Cl, and as
    this goes on Cl should end up containing a unique value in all

  - unify() returns 0 or 1 to inform if there are still several
    different values in Cl or if we have a single value in all
    positions, and we stop in this case.

I inserted debug information in the code, to be read like the example
below. This is one step of the output, it was run with ARGV[1] = 12
and shows the problem.

Change all 7 to 9                                           <- The elements in 
Cl to be changed
   1    2    3    4    5    6    7    8    9   10   11   12 <- Indexes i of Cl
   1    9    9    1    9  [ 7] [ 7] [ 7] [ 7]   9    9    9 <- Value of Cl[i],
                                                               elements to be 
changed are marked
   1    9    9    1    9    9    9    7    7    9    9    9 <- Value after 
changing all 7 to 9

This is real output, and you see that not all 7 are changed to 9. I
cannot explain the reasons for that, but in many cases only a few
elements are changed.

BTW, I solved the problem by copying the array Cl to another array
Aux, using Aux to replace elements and copying it back to Cl, but this
is not the best solution...

thanks for any hint,

joao batista

Attachment: xx.awk
Description: Text document

reply via email to

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