[solved] awk: test assoc. array for content

Hi all,

I am looking for a quick/short way in awk to check if an associative array has any content.
I know I can split() it to an indexed array and check if the 1st element is set, or cycle through it with something like for( ele in arr ) , but I want to avoid that, as I am looking for a shorter option.

If possible, I don't want to cycle through elements (I do not know what contents will be in there though or what key names will be used) - I would like to straight test.

Any ideas?

Considering the number of views of the thread and that I couldn't find anything in the documentations, I assume there is nothing like that. Meanwhile I just increased a hit-counter variable and check if it is set like:

...
/somepattern/ { hit_cnt[$3]++ }
...
END{
          ## Anything to process?
          for( anything in hit_cnt ){
                    start++
          }

          if( start ){
...

Hello zaxxon,

perl/python provide a function to determine the length of an array. I did a quick search for something similar and found this link.

I'm sure you would have come across these theories in your search... but just a re-iteration for discussion/documentation sake.

In case you stumble upon something better, it would be good to know that too. Thanks.

1 Like

Hi balajesuri,
thanks for the info! Indeed, length(hit_cnt) works for me since I am using GNU awk.