awk awk awk

Arrays 1

You may know what arrays are from previous programming experience - they are collections of related elements or objects.


As a programming language, awk can deal with arrays of data very well. So let's create an array to work with.

In a previous page on Scripting, we introduced the ability to write scripts, which can then be executed by awk. That's what we're going to do here to create our array.

Copy and paste the following code into a file and name it 'arrays1.awk':

BEGIN {
	awry["three"] = "blueberry"
	awry["four"] = "apple"
	awry["two"] = "cherry"
	awry["seven"] = "pineapple"
	awry["eight"] = "orange"
	awry["five"] = "grape"
	awry["six"] = "strawberry"
	awry[1] = "pear"
	print "\nUnsorted ...";
	for (i in awry) {
		print i " : " awry[i]
	}
}

That is an awk script, which you call when running awk:

 awk -f arrays1.awk
 

You will notice a few things that might seem odd. First, most of the indices are strings - there is one numeric index. How does that work?

We've also snuck in a loop construction. Not complicated but unique when dealing with an array.

When you run it in your shell, you should see something like this:

Unsorted ...
three : blueberry
seven : pineapple
two : cherry
five : grape
eight : orange
six : strawberry
four : apple
1 : pear

Another odd thing: these are not in the same order as the array!.

This shows us the difference between a loop index, and an array index.

When awk creates an array, the indices are consecutive integers beginning at 1. Note the loop is different from a regular 'for' loop - it is using the array index.

Sorting an array