我有两个文本文件,分别包含以下内容:
FILE1.txt
dog
cat
antelope
FILE2.txt
1
2
Barry
我想要实现的输出如下所示:
dog1
dog2
dogBarry
cat1
cat2
catBarry
antelope1
antelope2
antelopeBarry
我所采取的方式是:
open (FILE1, "<File1.txt") || die $!;
open (FILE2, "<File2.txt") || die $!;
my @animals = (<FILE1>); #each line of the file into an array
my @otherStrings = (<FILE2>); #each line of the file into an array
close FILE1 || die $!;
close FILE2 || die $!;
my @bothTogether;
foreach my $animal (@animals) {
chomp $animal;
foreach my $otherString (@otherStrings) {
chomp $otherString;
push (@bothTogether, "$animal$otherString");
}
}
print @bothTogether;
我现在用的方法是有效的,但我相信这不是最好的方法,特别是当文件可能包含数千行时?
那么做这件事的最佳方法是什么,也许使用哈希表?
my @animals = chomp(<FILE1>);
- Alexander Farber