An array allows you to work with fewer variable names by creating a collection of them and referring to them with a single name and a key. In the C language, when you talk of arrays, they are indexed with positive integer numbers; the awk language took another step ahead, as every array is an associative one.
Therefore, you define to a key a value. Imagine it as a collection of key-value pairs. Since in awk the default type is the string, the keys are also strings. This gives the advantage that the keys do not need to be a row of consecutive numbers. Possible construction syntaxes are (regardless of which one you use, the keys will be always converted to strings):
m[key]=value
m[1]="Yeti"
m["me"]= "Yeti"
a[2]=133
You can call a value if you know the key. If for some reason you call a key that does not exist, awk will not return an error. Instead, the return value will be an empty string. To iterate through the items of the array, you can use the following modified version of the "for" loop:
for (key_variable in array_name) commands;
For instance, in the case of the upper m array:
for ( i in m)
{
print m[i];
}
The items will appear in the order in which awk stored them and not in the order we defined them. This is a trait of associative arrays. It means that, if we order the items after some rule regarding the keys, we can access any item really fast later on. One thing is sure, the "for" loop will iterate through every item inside the array. If you want to delete an item from the array, use the "delete" keyword:
delete array_name[key];
However, if for some reason you do want to iterate through the array in some specific order, you must do this manually. First, you generate the keys for the array. The easiest way is to generate a consecutive number sequence. Use this as a key when you add new items. Finally, reusing the original number sequence, reiterate through the array. To find out whether or not a key is present in the array, use the following "if" construction:
if( key in array)
commands;
if (1 in m)
print We do have key 1 in the array;
Of course, during this approach we will use the classic "for" and just call the corresponding keys with the bracket parentheses. A classic example of how to combine the two in the case of sparse arrays is below:
{
#use numbers as keys
m[1]="Avril"
m[3]="Second"
m[5]="Final"
#find the borders
min=0; max =0;
for ( i in m)
if ( i < min)
min=x;
if(i>max)
max=x;
#now iterate through the borders with the classic if
for ( i = min; i<=max; ++i)
{
if( i in m)
{
print m[i]
}
}
}
It is also possible to decompose a string directly into an array. The syntax is as follows:
number = split( string, array_name, regular_expression)
Using the regular expression argument, the function will tokenize the string and store it in the array. Each token will be associated with a key in the array. The keys will be numbers starting from 1. The return value of the function is the number of tokens found.
echo 'First Second Final' | awk '{ n=split($0, m, / /);
print n "=>" m[1] m[2] m[3]'
Creating multiple arrays is also possible by giving multiple keys inside the brackets separated by commas.
{
m[3,9]=2;
m[9,3]=10;
print a[3,9] + a [9,3]
}
With this we have reached the end of this article. If you have paid close attention by now you should know enough to efficiently use the awk language and also be able to write basically any script inside it.
Naturally, what you've read here is not everything. Awk offers many more interesting aspects, but I will let you explore those on your own. As for UNIX, we will continue next time with process management. So if you are interested, make sure you rate this article, ask your questions if you have any, and tune in next time. Live With Passion!
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.