这是一个小问题,我希望你能帮助我。我的代码可能是垃圾。例如,我有一个文件,其中唯一的语句是John is the uncle of Sam
. 我的 Perl 脚本应该将文件内容复制到一个数组中。用户应该能够输入不同的名称并搜索文件中是否提到了这些名称。程序中应该有一个数组,如“叔叔阿姨、母亲、父亲等”。
#use warnings;
use Array::Utils qw(:all);
print "Please enter the name of the file\n";
my $c = <STDIN>;
open(NEW,$c) or die "The file cannot be opened";
@d = <NEW>;
print @d, "\n";
@g = qw(aunt uncle father);
chomp @d;
chomp @g;
my $e;
my $f;
print "Please enter the name of the first person\n";
my $a = <STDIN>;
print "Please enter the name of the second person\n";
my $b = <STDIN>;
my @isect = intersect(@g, @d);
print @isect;
foreach(@d)
{
if ($a == $_)
{
$e = $a;
}
else
{
print "The first person is not mentioned in the article";
exit();
}
if ($b == $_)
{
$f = $b;
}
else
{
print "The second person is not mentioned in the article";
exit();
}
}
print $e;
print $f;
close(NEW);
这是我到目前为止所做的事情,交集没有给出两个数组中常见的单词 uncle。该程序正在使用任何随机名称并打印它们。当我输入 John 和 Sam 以外的其他名称时,这并不是说文件中不存在该名称