gpt4 book ai didi

Perl 需要正确的 grep 运算符来匹配变量的值

转载 作者:行者123 更新时间:2023-12-02 07:06:25 26 4
gpt4 key购买 nike

我想看看我的数组中是否有重复的项目,有超过 16.000 个,所以将它自动化可能还有其他方法,但我从这个开始,好吧,除非有直接的命令,否则我想完成它。我正在做的是从一个数组转移并推送到另一个数组,这样,检查目标数组以查看它是否“在数组中”(就像 PHP 中有这样的命令一样)。

所以,我得到了这个子例程,它适用于文字,但不适用于变量。这是因为“情商”或我需要的任何东西。 “源文件”将包含目标数组中的一个或多个单词。

// Here I just fetch my file


$listamails = <STDIN>;
# Remove the newlines filename
chomp $listamails;
# open the file, or exit
unless ( open(MAILS, $listamails) ) {

print "Cannot open file \"$listamails\"\n\n";
exit;
}
# Read the list of mails from the file, and store it
# into the array variable @sourcefile
@sourcefile = <MAILS>;
# Close the handle - we've read all the data into @sourcefile now.
close MAILS;


my @destination = ('hi', 'bye');

sub in_array
{
my ($destination,$search_for) = @_;
return grep {$search_for eq $_} @$destination;
}

for($i = 0; $i <=100; $i ++)

{
$elemento = shift @sourcefile;
if(in_array(\@destination, $elemento))
{
print "it is";
}
else
{
print "it aint there";
}
}

好吧,如果我没有在其中包含 $elemento 而不是在其中放置一个“hi”,它确实可以工作,而且我还打印了 $elemento 的值,它也是“hi”,但是当我放置变量时,它不起作用工作,那是因为'eq',但我不知道还能放什么。如果我输入 ==,它会提示“hi”不是数值。

最佳答案

当您想要不同的值时,请考虑哈希。

my %seen;
@seen{ @array } = ();

if (keys %seen == @array) {
print "\@array has no duplicate values\n";
}

关于Perl 需要正确的 grep 运算符来匹配变量的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11274391/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com