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.


  1. Big Data and Hadoop is an ecosystem of open source components that fundamentally changes the way enterprises store, process, and analyze data.

    hadoop training in bangalore

  2. myTectra Amazon Web Services (AWS) certification training helps you to gain real time hands on experience on AWS. myTectra offers AWS training in Bangalore using classroom and AWS Online Training globally. AWS Training at myTectra delivered by the experienced professional who has atleast 4 years of relavent AWS experince and overall 8-15 years of IT experience. myTectra Offers AWS Training since 2013 and retained the positions of Top AWS Training Company in Bangalore and India.

    aws training in bangalore

  3. Harvard Business Review named data scientist the "sexiest job of the 21st century".This Data Science course will cover the whole data life cycle ranging from Data Acquisition and Data Storage using R-Hadoop concepts, Applying modelling through R programming using Machine learning algorithms and illustrate impeccable Data Visualization by leveraging on 'R' capabilities.With companies across industries striving to bring their research and analysis (R&A) departments up to speed, the demand for qualified data scientists is rising.

    data science training in bangalore