New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
for(i in array) not sequential #171
Comments
Nice catch, can confirm (5e49ea4). |
n = length(array)
for (i = 1; i <= n; i++)
do something with array[i] This should only be used when you know for sure that the indices are sequential (such as with Closing this issue. |
Well, you could of course have different opinions on this. When an array is indexed by an integer sequence a good design would take them in order. Of course there is always workarounds. BRMagnusSkickat från min iPhone10 feb. 2023 kl. 15:19 skrev Arnold Robbins ***@***.***>:
awk purposely does not define the order in which a for (i in array) loop goes through the array. You cannot depend on it to be "sequential", and different implementations will go through the loop in different orders. If you require sequential traversal, do it like so:
n = length(array)
for (i = 1; i <= n; i++)
do something with array[i]
This should only be used when you know for sure that the indices are sequential (such as with split()) since indices can be strings, or even be missing.
Closing this issue.
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: ***@***.***>
|
Possible to revisit the decision here? I'd argue for several points:
|
Hello.
Not really, no. The array management isn't going to change.
So this is arguing against ordered traversal of the array.
If by "original" you mean this version, you are incorrect. It has supported
This isn't necessary. If you know that an array is indexed from 1 to N, you can do this: for (i = 1; i in array; i++) ...
So this also argues against trying to provided ordered traversal of arrays.
Arrays are implemented using hash tables. What you're seeing is how things hash. Since the number of items in the array is small, it looks like it's sequential, but if you put in a lot of elements (say 100), you'll see that the order isn't sequential at all. In short, there's no bug here. As described, ordered traversal isn't so simple. Gawk provides ways to do it. It isn't the default in I hope all this helps. Thanks. |
on MacOS,awk version 20200816:
echo "one;three;54;3;86;seven" | awk '{split($0,a,";");for(i in a){print "a[" i "]=" a[i] }}'
a[2]=three
a[3]=54
a[4]=3
a[5]=86
a[6]=seven
a[1]=one
The text was updated successfully, but these errors were encountered: