I wonder if there is a way to check if a value exists in an associative array in awk, I mean without knowing the corresponding key and without looping through all the array, like we would have in Python dict.values(). Let's take this example where the last if statement -if ("2" in array)- is FALSE and I don't know how to write it so it's TRUE.
echo -e 'a\na\nb' | \
awk '
{ array[$1] += 1 }
END {
for (x in array){
# key, key[value]
print x, array[x]
}
if ("a" in array){
# acces key
print "OK for a key"
}
if ("2" == array["a"]){
# access key[value]
print "OK for a value when knowing the key"
}
if ("2" in array){
# access value without knowing key ?
print "OK for any value"}
} else {
print "ERROR"
}}'
Output:
a 2
b 1
OK for a key
OK for a value when knowing the key
ERROR
Is there a way to check if the array contains the values ("1" or "2" in this example) ?
Thanks !