how to check if awk array is empty

awk, linux, unix

Solution

Check it with `length()` function:

if ( length(sourceAndRestricted) > 0 ) {
    printf "\t\t"var"\n"
}
else
    print "NONE"
}

Problem

I am brand new to AWK and trying to determine if my array is empty or not so i can print a message if so. Typically i am use to length functions and can check like that, but it does not seem AWK has those. Here is my working code, i just want to print out a different message if there is nothing in the array after parsing all my data. ``` #add to array if condition is met if ($2 == "SOURCE" && $4 == "RESTRICTED"){ sourceAndRestricted[$3]++; } #print out array for (var in sourceAndRestricted){ printf "\t\t"var"\n" } ``` ive tried something like this and its not working. Suggestions? ``` for (var in sourceAndRestricted){ if (var > 1){ printf "\t\t"var"\n" } else { print "NONE" } } ```

Original source