我是Perl的新手,并试图学习它。我有两个文件,'file1'和'file2',我需要找到'file1'中哪些符号不在'file2'中为companyA和部门B和C。
File1中
GTY
TTY
UJK
TRE
文件2
departmentA_companyA.try=675 UJK 88 KKR
departmentA_companyB.try=878 UJK 37 TAR
departmentA_companyC.try=764 UJK 92 PAM
departmentB_companyA.try=675 UJK 88 KKR
departmentB_companyB.try=878 UJK 37 TAR
departmentB_companyC.try=764 UJK 92 PAM
departmentC_companyA.try=675 UJK 88 KKR
departmentC_companyB.try=878 UJK 37 TAR
departmentC_companyC.try=764 UJK 92 PAM
答案 0 :(得分:3)
在这种情况下,我建议您使用哈希的键来存储此列表($symbols{$symbol} = 1;
)。这是因为从哈希(delete $symbols{$symbol};
)中删除它是容易和便宜的。
扰流:
use strict;
use warnings;
use feature qw( say );
my %symbols;
{
open(my $fh, '<', 'file1')
or die("Can't open file1: $!\n");
while (<$fh>) {
chomp;
++$symbols{$_};
}
}
{
open(my $fh, '<', 'file2')
or die("Can't open file2: $!\n");
while (<$fh>) {
chomp;
my ($key, $val) = split /=/;
my ($dept, $co) = split /[_\.]/, $key;
if ($co eq 'companyA' || $dept eq 'departmentB' || 'departmentC') {
my @symbols = split ' ', $val;
delete @symbols{@symbols};
}
}
}
say for keys %symbols;
答案 1 :(得分:2)
您可以使用哈希计算每个符号在文件中出现的次数,然后打印计数为0的那些。
use strict;
open SYMS, $ARGV[0] || die;
open INFILE, $ARGV[1] || die;
my %symbols;
while (<SYMS>) {
chomp;
$symbols{$_} = 0;
}
while (<INFILE>) {
my @F=split;
next unless $F[0] =~ /companyA/;
next unless $F[0] =~ /department[BC]/;
++$symbols{$F[1]} if (defined $symbols{$F[1]});
++$symbols{$F[3]} if (defined $symbols{$F[3]});
}
for my $symbol (keys %symbols) {
print "$symbol\n" if $symbols{$symbol} == 0;
}