Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How define array on awk

Tags:

arrays

awk

I am looking for simple array definition on awk by simple example. How to define array and use the elements of the array on awk language?

like image 526
alex Avatar asked Mar 18 '23 23:03

alex


1 Answers

Awk does not have arrays, but maps.

Like all variables in awk, there is no need to define it. It will happen when you first use it.

To assign an element of a map:

a[key] = value

To use an element:

print a[key]

To iterate:

for (i in a) {
    print i, a[i]
}

If you use integers as keys, the map will be equivalent to an array.

like image 171
user000001 Avatar answered Mar 27 '23 20:03

user000001