Rereading a file 60,000 times is likely to slow things down somewhat. The trick is to read each file only once. To realise the trick you need to get the information you need from the smaller file and store it in memory. In this case a hash is probably the best way to store it because you can test very quickly for the match condition (assuming you don't actually require a regex match). Consider:
use strict; use warnings; # Sample data my $catFile = <<CAT; 123 234 345 678 CAT my $dataFile = <<DATA; A|12|r|some|56|78|90 E|123|r|some|56|78|90 D|678|r|some|56|78|90 C|12|r|some|56|78|90 F|345|r|y|98|0|0 DATA # Build the hash open my $catIn, '<', \$catFile; my %keys = map {chomp; $_ => 1} <$catIn>; close $catIn; open my $dataIn, '<', \$dataFile; while (<$dataIn>) { chomp; my @parts = split /\|/; next unless exists $keys{$parts[1]}; print join ('|', @parts), "\n"; } close($dataIn);
Prints:
E|123|r|some|56|78|90 D|678|r|some|56|78|90 F|345|r|y|98|0|0
In reply to Re: Compare 2 files and create a new one if it matches
by GrandFather
in thread Compare 2 files and create a new one if it matches
by shawshankred
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |