【问题标题】:Perl read a file and an array and find common wordsPerl 读取文件和数组并查找常用词
【发布时间】:2013-08-05 11:38:17
【问题描述】:

这是一个小问题,希望您能帮助我。我的代码可能是垃圾。例如,我有一个文件,其中唯一的语句是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 以外的其他名称时,这并不是说文件中不存在该名称

【问题讨论】:

    标签: arrays perl intersection words


    【解决方案1】:

    有几个问题:

    1. 你没有chomp$c。文件名末尾包含换行符。

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

    3. 您使用== 来比较字符串。字符串相等性使用eq 测试,但== 测试数字。

    4. 此外,您不想知道“Sam”是否等于“John is the uncle of Sam”。你想知道它是否是它的一部分。您可能需要使用index 或正则表达式来查找。

    5. 不要使用$a作为变量名,它是特殊的(见perlvar)。

    【讨论】:

    • eq 不能代替 == ... 程序能够读取文件并将其内容保存在数组中。我将 $a 和 $b 分别更改为第一和第二。我吃过 $c。
    • @potterbond007:当然,见 4。
    【解决方案2】:

    不要尝试将字符串与== 进行比较!请改用eq(等于)。你也没有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";
    }
    

    【讨论】:

    • 仍然说我给 John 和 Sam 起名字时没有提到这些人
    • 我明白了。 eq 测试您需要模式/正则表达式 (=~) 匹配的完全匹配!
    • 那么现在,我们需要@g 做什么?
    • 我实际上已经完成了正则表达式位。对于其余部分,我发布了一个不同的问题。感谢您的帮助。太棒了。
    猜你喜欢
    • 1970-01-01
    • 2016-08-01
    • 2015-10-18
    • 1970-01-01
    • 1970-01-01
    • 2021-01-16
    • 1970-01-01
    • 1970-01-01
    • 2019-05-22
    相关资源
    最近更新 更多