Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

for (var in array) - random order??

Status
Not open for further replies.
Jun 22, 2000
6,317
AU
Is it just me or were A, W or K sadistic?

Trying this code in various awks gives surprising results... surprising to me anyway.

Code:
BEGIN {
        a[0]="zero"
        a[1]="one"
        a[2]="two"
        a[3]="three"
        a[4]="four"
        a[5]="five"
        a[6]="six"
        a[7]="seven"
        a[8]="eight"
        for (v in a) print v,a[v];
}

Linux gawk 3.1.0

[tt]4 four
5 five
6 six
7 seven
8 eight
0 zero
1 one
2 two
3 three[/tt]

Solaris 8 awk

[tt]2 two
3 three
4 four
5 five
6 six
7 seven
8 eight
0 zero
1 one[/tt]

Solaris 8 nawk

[tt]2 two
3 three
4 four
5 five
6 six
7 seven
8 eight
0 zero
1 one[/tt]

Solaris 8 /usr/xpg4/bin/awk

[tt]8 eight
7 seven
6 six
5 five
4 four
3 three
2 two
1 one
0 zero[/tt]

SCO OpenServer awk

[tt]2 two
3 three
4 four
5 five
6 six
7 seven
8 eight
0 zero
1 one[/tt]

And if I try adding an asort(a) before the for loop in the gawk version, the results are even stranger!

[tt]4 one
5 seven
6 six
7 three
8 two
9 zero
1 eight
2 five
3 four[/tt]

Note the '9' index that has suddenly appeared?

So let's say I wanted to iterate through the array in order... I'd have to use a C-style for (i=0; i<something; i++) loop... but then, how do I know how many elements are in the array? There is no handy function to return that number... although I notice asort() does it, but only as a side-effect.

Freaky.

Annihilannic.
 
for(v=0;v in a;++v) print v,a[v]

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
All arrays in Awk are associative arrays. (Ruby and Perl have normal arrays in addition to associative arrays, which are known as hashes.) That's why you can use [tt]"Foo, but not bar!!!"[/tt] as an array index. The order in which the indexes (keys) are returned depends on the hash function and the number of buckets.

To use asort():
Code:
        asort(a)
        for (i=1;i in a; i++)  print a[i]
produces
[tt]
eight
five
four
one
seven
six
three
two
zero
[/tt]
The values are in sorted order.
 
Hi

Annihilannic said:
Is it just me or were A, W or K sadistic?
If they are, then L.W. is sadistic too, because associative arrays works the same way in Perl :
Code:
$a{0}="zero";
$a{1}="one";
$a{2}="two";
$a{3}="three";
$a{4}="four";
$a{5}="five";
$a{6}="six";
$a{7}="seven";
$a{8}="eight";
foreach $v (keys %a) { print $v," ",$a{$v},"\n"; }
Perl 5.8.0 :

6 six
3 three
7 seven
2 two
8 eight
1 one
4 four
0 zero
5 five

I think their choice for this behaviour was motivated by better performance possibilities. Of course, this is a pain when you have only associative arrays.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top