arrays - 使用 Perl 比较一组字符串和一个文件

标签 arrays regex perl

我正在尝试编写一个 Perl 脚本,它将找出一组字符串和一个文件之间的差异,并且我想打印与字符串不匹配的文件内容。

我的 INPUT1 将类似于:(字符串集)

AAAAA
BBBBB
CCCCC
DDDDD
EEEEE   --- These are user ids which should be passed in the script

我的INPUT2将是一个名为User.txt的文件,其中有许多ID,包括上面提到的ID

ABBAAA
ACARVAV
AAAAA
BBBBB
CCCCC
DDDDD
EEEEE
BGATA
ETYUIOL

我希望我的输出是这样的

ABBAAA
ACARVAV
BGATA
ETYUIOL

到目前为止我已经到达

my @things_to_find = qw(AAAAAA BBBBB CCCCC DDDDD EEEEE);
my $comparefile = "User.txt";
open ( my $compare_filehandle, "<", $comparefile ) or die $!;
while ( my $line = <$compare_filehandle> ) 
{
    foreach my $thing ( @things_to_find )
    {
        print "Match found with: $line" if $line !~ /$thing/;
    }
}

但这并没有产生所需的输出。我对 Perl 很陌生,所以您的任何建议对我都会非常有帮助。

最佳答案

尝试一下:

use List::Util qw(none);
my @things_to_find = qw(AAAAAA BBBBB CCCCC DDDDD EEEEE);
my $comparefile = "User.txt";
open ( my $compare_filehandle, "<", $comparefile ) or die $!;
while ( my $line = <$compare_filehandle> ) 
{
    print $line if none { $line =~ /\b$_\b/}  @things_to_find;
}

文档List::Util

关于arrays - 使用 Perl 比较一组字符串和一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25307008/

相关文章:

perl - 用 perl 分割线

JavaScript 关联数组 :/

javascript - 使用模式的html5时间验证问题

regex - str_extract_all 返回不匹配的组

c# - 匹配 FOO 连字符和数字的复杂正则表达式

code-coverage - 运行 `cover -test` 时如何从子进程收集覆盖率,否则不收集? (开发::封面)

perl - 比较两个十进制值与perl中的特定范围

php - 如何将数组从经典asp发送到php页面中的表?

arrays - 从向量中获取向量矩阵

python - 从子数组中的 numpy 二维数组中提取相交数组的索引