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

时间:2021-01-29 12:15:49

I have two arrays one is fix to be 8 letters and the other depends on the user. I have to take user input and put in an array (done) but I need to check if the users input (it is a word) letters are in the other array ? how can I do it ?

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

1 个解决方案

#1


4  

You can use Perl's (v5.10+) smartmatch operator ~~ to check if a string is an element of an array. The matching is case-sensitive:

您可以使用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.'

Sample run:

示例运行:

Enter a word: This
The word "This" is in @words.

#1


4  

You can use Perl's (v5.10+) smartmatch operator ~~ to check if a string is an element of an array. The matching is case-sensitive:

您可以使用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.'

Sample run:

示例运行:

Enter a word: This
The word "This" is in @words.