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.
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.
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.
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.
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.
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 $
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With