检查数组包含值 [英] Check if array contains value

查看:108
本文介绍了检查数组包含值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用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?

推荐答案


雷神的评论
这个函数的伎俩,我

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

#!/usr/bin/awk -f
function smartmatch(small, large,    values) {
  for (each in large)
    values[large[each]]
  return small in values
}
BEGIN {
  split("444 555 666", foo)
  print smartmatch(555, foo) ? "T" : "F"
}

这篇关于检查数组包含值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆