Arrays 使用Perl比较一组字符串和一个文件
我正在尝试编写一个Perl脚本,它将找出一组字符串和一个文件之间的差异,我想打印与字符串不匹配的文件内容 我的输入1将类似:(一组字符串) 我的INPUT2将是一个名为User.txt的文件,它有许多id,包括上面提到的idArrays 使用Perl比较一组字符串和一个文件,arrays,regex,perl,Arrays,Regex,Perl,我正在尝试编写一个Perl脚本,它将找出一组字符串和一个文件之间的差异,我想打印与字符串不匹配的文件内容 我的输入1将类似:(一组字符串) 我的INPUT2将是一个名为User.txt的文件,它有许多id,包括上面提到的id ABBAAA ACARVAV AAAAA BBBBB CCCCC DDDDD EEEEE BGATA ETYUIOL 我希望我的输出像 ABBAAA ACARVAV BGATA ETYUIOL 到目前为止,我已到达目的地 my @things_to_find = qw(
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/;
}
}
my@things\u to\u find=qw(AAAAAA BBBBB CCCCC ddd eee);
my$comparefile=“User.txt”;
打开(my$compare_filehandle,“使用strict;
使用警告;
使用自动模具;
打开我的$in,你可以尝试这个简单的grep
匹配模式
use strict;
use warnings;
use autodie;
my @users = qw(AAAAAA BBBBB CCCCC DDDDD EEEEE);
my $file = "User.txt";
open my $fh, "<", $file;
while ( my $line = <$fh> ) {
chomp $line;
print "Matched line : $line\n" unless grep {$line eq $_} @users;
}
使用严格;
使用警告;
使用自动模具;
my@users=qw(AAAAA BBBBB CCCCDDD EEE);
my$file=“User.txt”;
打开我的$fh,“试试:
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;
}
使用列表::Util qw(无);
我的@things_to_find=qw(AAAAAA BBBBB CCCCC ddd eee);
my$comparefile=“User.txt”;
打开(my$compare_文件句柄,"目前,您自己的代码会查找文件中不包含列表中所有字符串的所有行,但该行不等于这些字符串中的任何一个。您需要将包含测试更改为相等测试;找到匹配项后立即跳过该字符串;并使用chomp
从字符串中删除尾随的换行符从文件中删除
有两种明显的编写方法。第一种方法是构建哈希,它实际上是一个由字符串而不是整数索引的数组。如果用文件中的条目填充哈希,然后删除字符串数组中的条目,则如下所示
use strict;
use warnings;
my $comparefile = 'User.txt';
my @users = qw/ AAAAA BBBBB CCCCC DDDDD EEEEE /;
open my $users_fh, '<', $comparefile or die $!;
my %file_users;
while (my $user = <$users_fh> ) {
chomp $user;
$file_users{$user} = 1;
}
delete $file_users{$_} for @users;
print "$_\n" for sort keys %file_users;
另一种方法是从字符串中构建正则表达式,并使用它从文件中选择要忽略的行。这类似于下面的程序,并且输出与上一个程序相同。此解决方案速度更快,但包含一些更高级的思想,如正则表达式和map
use strict;
use warnings;
my $comparefile = 'User.txt';
my @users = qw/ AAAAA BBBBB CCCCC DDDDD EEEEE /;
my $re = join '|', map "^\Q$_\E\$", @users;
$re = qr/$re/;
open my $users_fh, '<', $comparefile or die $!;
my @file_users;
while (my $user = <$users_fh> ) {
chomp $user;
push @file_users, $user unless $user =~ $re;
}
print "$_\n" for sort @file_users;
使用严格;
使用警告;
my$comparefile='User.txt';
my@users=qw/aaaaaabbbbb CCCCC ddd EEEEE/;
my$re=加入“|”,映射“^\Q$\ue\$”,@users;
$re=qr/$re/;
打开我的$users\u fh,'如果文件中有bbbbb c
,该怎么办?@robarl:Good catch,修复了。
use strict;
use warnings;
my $comparefile = 'User.txt';
my @users = qw/ AAAAA BBBBB CCCCC DDDDD EEEEE /;
open my $users_fh, '<', $comparefile or die $!;
my %file_users;
while (my $user = <$users_fh> ) {
chomp $user;
$file_users{$user} = 1;
}
delete $file_users{$_} for @users;
print "$_\n" for sort keys %file_users;
ABBAAA
ACARVAV
BGATA
ETYUIOL
use strict;
use warnings;
my $comparefile = 'User.txt';
my @users = qw/ AAAAA BBBBB CCCCC DDDDD EEEEE /;
my $re = join '|', map "^\Q$_\E\$", @users;
$re = qr/$re/;
open my $users_fh, '<', $comparefile or die $!;
my @file_users;
while (my $user = <$users_fh> ) {
chomp $user;
push @file_users, $user unless $user =~ $re;
}
print "$_\n" for sort @file_users;