我有两个包含以下内容的文本文件:
文件1.txt
dog
cat
antelope
文件2.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;
我完成它的方式有效,但我确信这不是最好的方式,特别是当文件都可能包含数千行时?
这样做的最好方法是什么,也许使用哈希?