Thursday, December 16, 2010

Arrays in nawk: test for existance and implicit creation of elements

When using arrays in nawk I still stumble upon handling the test for existance wrongly.

Consider this example:

nawk '{for(i in u) print "1",i,u[i]; if(u["ab"]!="") print "ab exists"; for(i in u) print "2",i,u[i]}'

This script should print all entries in array u then do a check if u["ab"] is non-empty and again print all entries in array u.

Since there is no obvious assignment to u[] the assumption is that neither of the loops will print anything so it is somewhat unexpected that the first line of input will trigger a line of output:

2 ab
i.e. the second loop finds an array element ab. Why?

Because the test u["ab"]!="" implicitly created the array entry u["ab"] with an empty content.

The correct way to test for existance which does not create an entry implicitly is like this:
nawk '{for(i in u) print "1",i,u[i]; if("ab" in u) print "ab exists"; for(i in u) print "2",i,u[i]}'

Rule: never test with array[ind]=="". Always use ind in array.

No comments:

Post a Comment