检查awk数组是否包含值

问题描述:

使用Perl,您可以检查数组是否包含值

With Perl you can check if an array contains a value

$ perl -e '@foo=(444,555,666); print 555 ~~ @foo ? "T" : "F"'
T

但是,使用awk时,此类似命令正在检查数组索引,而不是检查数组索引 值

However with awk, this similar command is checking the array indexes rather than values

$ awk 'BEGIN {split("444 555 666", foo); print 555 in foo ? "T" : "F"}'
F

如何使用awk检查数组是否包含特定值?

How can I check if an array contains a particular value with awk?

基于 Thor的评论,此功能可以解决我:

Based on Thor’s comment, this function does the trick for me:

function smartmatch(diamond, rough,   x, y) {
  for (x in rough) y[rough[x]]
  return diamond in y
}
BEGIN {
  split("444 555 666", z)
  print smartmatch(555, z) ? "T" : "F"
}