Your approach will work just fine for files with thousands of lines. It really is not that important. For millions of lines, this can be a problem.
However, you could reduce the memory usage in your code by simply reading one file in memory, and print the results immediately, rather than storing them in an array:
use warnings; use strict; open my $animals, '<', 'File1.txt' or die "Can't open animals: $!"; open my $payloads, '<', 'File2.txt' or die "Can't open payloads: $!"; my @payloads = <$payloads>;
With two huge files of the same size, this will be approximately 1/4 of the memory of your source code.
Update: I also edited the code to include Simbabque in good suggestions for upgrading it.
Update 2:. As others noted, you could not read a single file in memory, going through the payload file line by line in each line of the animal file. However, this will be much slower. It should be avoided if absolutely necessary. The approach I suggested will be about the same as the source code.
user1919238
source share