我有一组 ID。我有一个 ID,我想查找该 ID 是否存在于 Perl 的 ID 数组中
我尝试了以下代码:
my $ids = [7,8,9];
my $id = 9;
foreach my $new_id (@$ids) {
if ($new_id == $id) {
print 'yes';
} else {
print 'no';
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的输出为:
nonoyes
Run Code Online (Sandbox Code Playgroud)
相反,我只想获得输出:
yes
Run Code Online (Sandbox Code Playgroud)
由于 ID 存在于 ID 数组中
任何人都可以帮忙吗?
提前致谢
小智 5
my $ids = [7,8,9];
my $id = 9;
if (grep $_ == $id, @ids) {
print $id. " is in the array of ids";
} else {
print $id. " is NOT in the array";
}
Run Code Online (Sandbox Code Playgroud)