Search code examples
awkkeyassociative-array

How can I get the list of indices an associative array has?


I have an associative array in awk, and I can iterate over the values of the array using (e.g.) for (v in ARRAY) ....

However I want to iterate over the indices (keys) of the array instead.

Unfortunately I did not find the function how to do that. (In Perl I'd use keys %hash for that)


Solution

  • I have an associative array in awk, and I can iterate over the values of the array using (e.g.) for (v in ARRAY) ....

    As far I as know it will do iterate over keys, consider following example

    awk 'BEGIN{arr["a"]=1;arr["b"]=2;arr["c"]=3}END{for(i in arr){print i}}'
    

    output

    a
    b
    c
    

    (tested in gawk 4.2.1)