如何检查一个数组的元素是否在另一个数组中?

时间:2012-11-22 01:19:58

标签: arrays perl

我有两个阵列,一个是固定为8个字母,另一个取决于用户。我必须接受用户输入并放入一个数组(完成)但我需要检查用户输入(这是一个单词)字母是否在另一个数组中?我怎么能这样做?

1 个答案:

答案 0 :(得分:4)

您可以使用Perl(v5.10 +)smartmatch运算符~~来检查字符串是否是数组的元素。匹配区分大小写:

use strict;
use warnings;

my @words = map lc, qw/This is a test/;

print 'Enter a word: ';
chomp( my $entry = <> );

print qq{The word "$entry" is}
  . ( lc $entry ~~ @words ? '' : ' not' )
  . ' in @words.'

示例运行:

Enter a word: This
The word "This" is in @words.
相关问题