我有一组 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';
}
}
我得到的输出是:
nonoyes
相反,我只想获得输出:
yes
因为 ID 存在于 ID 数组中
有人可以帮忙吗?
提前致谢
最佳答案
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";
}
关于arrays - 无法找到项目数组中是否存在一项并在 Perl 中返回必要的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59576379/