Finding symbols in files using Perl
I am new to Perl and trying to learn it. I have two files, 'file1' and 'file2', I need to find which symbols in 'file1' are not in 'file2' for companyA and departments B and C.
File1
GTY
TTY
UJK
TRE
File2
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
d开发者_运维知识库epartmentB_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
- Create a list of all the symbols in file1
- Go through file2. If the criteria matches, delete the symbol from the list.
In this case, I'd suggest you use the keys of a hash to store this list ($symbols{$symbol} = 1;
). This is because it's easy and cheap to delete from a hash (delete $symbols{$symbol};
).
Spoiler:
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;
You can use a hash to count the number of times each symbol appears in the file, then print the ones that have a count of 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;
}
精彩评论