Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays Perl读取一个文件和一个数组并查找常用词_Arrays_Perl_Intersection_Words - Fatal编程技术网

Arrays Perl读取一个文件和一个数组并查找常用词

Arrays Perl读取一个文件和一个数组并查找常用词,arrays,perl,intersection,words,Arrays,Perl,Intersection,Words,这是一个小问题,我希望你能帮助我。我的代码可能是垃圾。例如,我有一个文件,其中唯一的语句是约翰是山姆大叔。我的Perl脚本应该将文件内容复制到数组中。用户应该能够输入不同的名称,并在文件中提到这些名称时进行搜索。节目中应该有“叔叔、婶婶、母亲、父亲等”的关系数组 #use warnings; use Array::Utils qw(:all); print "Please enter the name of the file\n"; my $c = <STDIN>; open(N

这是一个小问题,我希望你能帮助我。我的代码可能是垃圾。例如,我有一个文件,其中唯一的语句是
约翰是山姆大叔
。我的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);
#使用警告;
使用数组::Utils qw(:all);
打印“请输入文件名\n”;
我的$c=;
打开(新,$c)或死“文件无法打开”;
@d=;
打印@d,“\n”;
@g=qw(阿姨叔叔父亲);
chomp@d;
chomp@g;
我的$e;
我的$f;
打印“请输入第一个人的姓名\n”;
我的$a=;
打印“请输入第二个人的姓名\n”;
我的$b=;
my@isect=intersect(@g,@d);
打印@isect;
foreach(@d)
{
如果($a=$)
{
$e=$a;
}
其他的
{
打印“文章中未提及第一人”;
退出();
}
如果($b=$)
{
$f=$b;
}
其他的
{
打印“文章中未提及第二个人”;
退出();
}
}
打印$e;
打印$f;
关闭(新);

这是我到目前为止已经做过的事情,交叉点并没有给出单词“叔叔”,这是两个数组中常用的单词。该程序将随机抽取任何名称并打印它们。当我输入除John和Sam以外的其他名称时,并不是说文件中不存在该名称,而是存在以下几个问题:

  • 您不必
    chomp
    $c。文件名末尾包含一个换行符

  • 您使用
    open
    的双参数形式,但不测试第二个参数。这是一个安全问题:您知道如果用户输入包含
    |
    会发生什么吗

  • 您可以使用
    ==
    来比较字符串。使用
    eq
    测试字符串相等性,但是,
    ==
    测试数字

  • 此外,你不想知道“山姆”是否等于“约翰是山姆的叔叔”。你想知道它是否是它的一部分。您可能需要使用
    索引
    或正则表达式来查找

  • 不要使用
    $a
    作为变量名,它是特殊的(请参阅)


  • 有几个问题:

  • 您不必
    chomp
    $c。文件名末尾包含一个换行符

  • 您使用
    open
    的双参数形式,但不测试第二个参数。这是一个安全问题:您知道如果用户输入包含
    |
    会发生什么吗

  • 您可以使用
    ==
    来比较字符串。使用
    eq
    测试字符串相等性,但是,
    ==
    测试数字

  • 此外,你不想知道“山姆”是否等于“约翰是山姆的叔叔”。你想知道它是否是它的一部分。您可能需要使用
    索引
    或正则表达式来查找

  • 不要使用
    $a
    作为变量名,它是特殊的(请参阅)


  • 不要尝试将字符串与
    ==
    进行比较!改用
    eq
    (equals)。另外,您也没有
    chomp
    您的输入
    $a
    $b`。我想这就是你想要做的:

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    print "Please enter the name of the file\n";
    my $c = <STDIN>;
    
    open(NEW,$c) or die "The file cannot be opened";
    
    my @d = <NEW>;
    chomp @d;
    my $e;
    my $f;
    
    
    print "Please enter the name of the first person\n";
    my $aa = <STDIN>;
    print "Please enter the name of the second person\n";
    my $bb = <STDIN>;
    
    chomp $aa;
    chomp $bb;
    
    my $pattern_a = quotemeta $aa;
    my $pattern_b = quotemeta $bb;
    
    foreach (@d){
    
        if ($_ =~ /$pattern_a/){
            $e = $aa;
        }
        elsif ($_ =~ /$pattern_b/){
            $f = $bb;
        }
    }
    
    close(NEW);
    
    
    unless ($e){
        print "First person not mentionend\n";
    }
    unless ($f){
        print "Second person not mentioned\n";
    }
    
    #/usr/bin/perl
    严格使用;
    使用警告;
    打印“请输入文件名\n”;
    我的$c=;
    打开(新,$c)或死“文件无法打开”;
    我的@d=;
    chomp@d;
    我的$e;
    我的$f;
    打印“请输入第一个人的姓名\n”;
    我的$aa=;
    打印“请输入第二个人的姓名\n”;
    我的$bb=;
    咀嚼$aa;
    咀嚼$bb;
    my$pattern_a=quotemeta$aa;
    我的$pattern_b=quotemeta$bb;
    foreach(@d){
    如果($\u=~/$pattern\u a/){
    $e=$aa;
    }
    elsif($\=~/$pattern\u b/){
    $f=$bb;
    }
    }
    关闭(新);
    除非(e美元){
    打印“第一人称未提及结束\n”;
    }
    除非(f美元){
    打印“未提及的第二个人\n”;
    }
    
    不要尝试将字符串与
    =
    进行比较!改用
    eq
    (equals)。另外,您也没有
    chomp
    您的输入
    $a
    $b`。我想这就是你想要做的:

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    print "Please enter the name of the file\n";
    my $c = <STDIN>;
    
    open(NEW,$c) or die "The file cannot be opened";
    
    my @d = <NEW>;
    chomp @d;
    my $e;
    my $f;
    
    
    print "Please enter the name of the first person\n";
    my $aa = <STDIN>;
    print "Please enter the name of the second person\n";
    my $bb = <STDIN>;
    
    chomp $aa;
    chomp $bb;
    
    my $pattern_a = quotemeta $aa;
    my $pattern_b = quotemeta $bb;
    
    foreach (@d){
    
        if ($_ =~ /$pattern_a/){
            $e = $aa;
        }
        elsif ($_ =~ /$pattern_b/){
            $f = $bb;
        }
    }
    
    close(NEW);
    
    
    unless ($e){
        print "First person not mentionend\n";
    }
    unless ($f){
        print "Second person not mentioned\n";
    }
    
    #/usr/bin/perl
    严格使用;
    使用警告;
    打印“请输入文件名\n”;
    我的$c=;
    打开(新,$c)或死“文件无法打开”;
    我的@d=;
    chomp@d;
    我的$e;
    我的$f;
    打印“请输入第一个人的姓名\n”;
    我的$aa=;
    打印“请输入第二个人的姓名\n”;
    我的$bb=;
    咀嚼$aa;
    咀嚼$bb;
    my$pattern_a=quotemeta$aa;
    我的$pattern_b=quotemeta$bb;
    foreach(@d){
    如果($\u=~/$pattern\u a/){
    $e=$aa;
    }
    elsif($\=~/$pattern\u b/){
    $f=$bb;
    }
    }
    关闭(新);
    除非(e美元){
    打印“第一人称未提及结束\n”;
    }
    除非(f美元){
    打印“未提及的第二个人\n”;
    }
    
    eq不起作用,而不是==。。。程序能够读取文件并将其内容保存在数组中。我将$a和$b分别更改为第一个和第二个。我已经吃了$c。@potterbond007:当然,请看4.eq不起作用,而不是==。。。程序能够读取文件并将其内容保存在数组中。我将$a和$b分别更改为第一个和第二个。我已经吃了$c。@potterbond007:当然,见4。仍然说我给John和SamI-see起名时没有提到这些人
    eq
    测试精确匹配您需要模式/regex(
    =~
    )匹配!那么现在,我们需要
    @g
    做什么呢?我实际上已经完成了正则表达式位。在接下来的时间里,我发布了一个不同的问题。谢谢你的帮助。这真是太棒了。但我还是说,当我给约翰和萨米的名字时,他们没有被提及
    eq
    测试精确匹配您需要一个模式/regex(