Perl中读取文件和一个数组,找到共同的词 [英] Perl read a file and an array and find common words

查看:174
本文介绍了Perl中读取文件和一个数组,找到共同的词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是种一个小问题,我希望你能帮助我。我的code可能是垃圾。举个例子,我有一个文件,其中唯一语句是约翰是山姆叔叔。我的Perl脚本应该复制文件内容到一个数组。用户应该能够输入不同的名称,如果这些名称在文件中提到的搜索。应该有像在节目中伯父伯母,母亲,父亲等关系的数组。

 #使用警告;
使用数组:: utils的QW(:所有);打印请输入文件\\ n的名;
我的$ C = LT; STDIN取代;开(NEW,$ c)或死的文件无法被打开;@d =<新型取代;
打印@d,\\ n;@g = QW(叔叔阿姨的父亲);终日啃食@d;
终日啃食@g;
我的$ê;
我的$ F;
打印请输入的第一人\\ n的名;
我的一个$ = LT; STDIN取代;
打印请输入第二人\\ n的名;
我的$ B = LT; STDIN取代;我@isect =相交(@g,@d);打印@isect;
的foreach(@d)
    {
        如果($ A == $ _)
            {
                $ E = $ A;
            }
        其他
            {
                打印第一人是不是在文章中提到
                出口();
            }
        如果($ B $ == _)
            {
                $ F = $ B;
            }
        其他
            {
                打印第二个人没有在文章中提到
                出口();
            }
    }
打印$ê;
打印$ F;
(新);

这是什么,我已经做了,到目前为止,交点是不给的话大叔这是在两个数组中常见的词。该计划采取任何随机名称进行打印。这是不是说,当我进入比约翰和萨姆等不同名称的名称没有在文件中存在


解决方案

有几个问题:


  1. 您不要格格 $ C。该文件名包含在最后一个换行符。


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


  3. 您使用 == 比较字符串。字符串的平等与 EQ ,虽然测试, == 测试号码。


  4. 此外,你不想知道山姆是否等于约翰是山姆叔叔。你想知道它是否是它的一部分。您可能需要使用首页或常规前pressions找出来。


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


This is kind of a small issue and I hope you are able to help me. My code is probably rubbish. For an example, I have a file in which the only statement is John is the uncle of Sam. My Perl script should copy the file contents into an array. User should be able to input different names and search if those names are mentioned in the file. There should be an array with relationships like "uncle aunt, mother, father etc" in the program.

#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);

This is something that I have done so far, the intersection is not giving the word uncle which is the word common in both arrays. The program is taking any random name and printing them. It is not saying that the name doesn't exist in the file when I enter a different name other than John and Sam

解决方案

There are several problems:

  1. You do not chomp $c. The filename contains a newline at the end.

  2. You use the 2-argument form of open, but do not test the second argument. This is a security problem: do you know what happens if the user input contains > or |?

  3. You use == to compare strings. String equality is tested with eq, though, == tests numbers.

  4. Moreover, you do not want to know whether "Sam" equals to "John is the uncle of Sam". You want to know whether it is a part of it. You might need to use index or regular expressions to find out.

  5. Do not use $a as the name of a variable, it is special (see perlvar).

这篇关于Perl中读取文件和一个数组,找到共同的词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆