Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize an array of arrays in awk?

Is it possible to initialize an array like this in AWK ?

Colors[1] = ("Red", "Green", "Blue") Colors[2] = ("Yellow", "Cyan", "Purple") 

And then to have a two dimensional array where Colors[2,3]="Purple".


From another thread I understand that it's not possible ( "sadly, there is no way to set an array all at once without abusing split()" ). Anyways I want to be 100% sure and I'm sure that there are others with the same question.

I am looking for the easiest method to initialize arrays like the one above, will be nice to have it well written.

like image 349
BearCode Avatar asked Dec 28 '12 02:12

BearCode


People also ask

How do you declare an array in awk?

AWK has associative arrays and one of the best thing about it is – the indexes need not to be continuous set of number; you can use either string or number as an array index. Also, there is no need to declare the size of an array in advance – arrays can expand/shrink at runtime.

Does awk have arrays?

The awk language provides one-dimensional arrays for storing groups of related strings or numbers. Every awk array must have a name. Array names have the same syntax as variable names; any valid variable name would also be a valid array name.

How do you use associative array in awk?

Awk Associative Array Associative arrays are like traditional arrays except they uses strings as their indexes rather than numbers. When using an associative array, you can mimic traditional array by using numeric string as index. In the above awk syntax: arrayname is the name of the array.

How do I sort an array in awk?

12.2 Controlling Array Traversal and Array Sorting In addition, two built-in functions, asort() and asorti() , let you sort arrays based on the array values and indices, respectively. These two functions also provide control over the sorting criteria used to order the elements during sorting.


Video Answer


2 Answers

You can create a 2-dimensional array easily enough. What you can't do, AFAIK, is initialize it in a single operation. As dmckee hints in a comment, one of the reasons for not being able to initialize an array is that there is no restriction on the types of the subscripts, and hence no requirement that they are pure numeric. You can do multiple assignments as in the script below. The subscripts are formally separated by an obscure character designated by the variable SUBSEP, with default value 034 (U+001C, FILE SEPARATOR). Clearly, if one of the indexes contains this character, confusion will follow (but when was the last time you used that character in a string?).

BEGIN {     Colours[1,1] = "Red"     Colours[1,2] = "Green"     Colours[1,3] = "Blue"     Colours[2,1] = "Yellow"     Colours[2,2] = "Cyan"     Colours[2,3] = "Purple" } END {     for (i = 1; i <= 2; i++)         for (j = 1; j <= 3; j++)             printf "Colours[%d,%d] = %s\n", i, j, Colours[i,j]; } 

Example run:

$ awk -f so14063783.awk /dev/null Colours[1,1] = Red Colours[1,2] = Green Colours[1,3] = Blue Colours[2,1] = Yellow Colours[2,2] = Cyan Colours[2,3] = Purple $ 
like image 28
Jonathan Leffler Avatar answered Oct 08 '22 20:10

Jonathan Leffler


If you have GNU awk, you can use a true multidimensional array. Although this answer uses the split() function, it most certainly doesn't abuse it. Run like:

awk -f script.awk 

Contents of script.awk:

BEGIN {      x=SUBSEP      a="Red" x "Green" x "Blue"     b="Yellow" x "Cyan" x "Purple"      Colors[1][0] = ""     Colors[2][0] = ""      split(a, Colors[1], x)     split(b, Colors[2], x)      print Colors[2][3] } 

Results:

Purple 
like image 132
Steve Avatar answered Oct 08 '22 18:10

Steve