Perl遍历文件中的每一行,并将其附加到另一个文件中每行的末尾 [英] Perl iterating through each line in a file and appending to the end of each line in another file

查看:1242
本文介绍了Perl遍历文件中的每一行,并将其附加到另一个文件中每行的末尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文本文件,其中包含以下内容:

FILE1.txt

  dog 
cat
antelope

FILE2.txt

  1 
2
Barry

我想要达到的输出如下所示:

  dog1 
dog2
dogBarry
cat1
cat2
catBarry
antelope1
antelope2
antelopeBarry

他们的方式我走了:

  open(FILE1,< File1.txt)||死! 
打开(FILE2,< File2.txt)||死!

my @animals =(< FILE1>); #将文件的每一行放入数组
my @otherStrings =(< FILE2>); #将文件的每一行放入数组

关闭FILE1 ||死!
关闭FILE2 ||死!

我的@bothTogether;
foreach my $ animal(@animals){
chomp $ animal;
foreach我的$ otherString(@otherStrings){
chomp $ otherString;
push(@bothTogether,$ animal $ otherString);
}
}
print @bothTogether;

我已经完成了这项工作,但我相信这不是最好的方法关于它,特别是当这些文件可能包含数千行文件时? 这样做的最好方法是什么?使用散列? 解决方案

您的方法对于具有数千行的文件可以很好地工作。那真的不是那么大。对于数百万行,这可能是一个问题。然而,您可以通过只读取一个文件到内存中来减少代码的内存使用量,以及打印结果,而不是将它们存储在数组中:

 使用警告; 
使用strict;

打开我的$动物,'<','File1.txt'或死无法打开动物:$!;
打开我的$有效载荷,'<','File2.txt'或死无法打开有效载荷:$!;

my @payloads =< $有效负载>; #将文件的每一行放入一个数组
关闭$ payloads或死掉无法关闭有效载荷:$!;

while(my $ line =< $ animals>){
chomp $ line;
print $ line。$ _ foreach(@payloads);
}
关闭$动物或死亡不能关闭动物:$!;

对于两个大小相同的大文件,这将使用大约原始代码的四分之一。



更新:我也编辑了代码,以包含Simbabque对其进行更新的良好建议。


$ b $更新2:正如其他人已经注意到的,您可以将任何文件都读入内存,在动物文件的每一行上逐行读取有效载荷文件。但是,这会慢很多。除非绝对必要,否则应该避免。我建议的方法与原始代码速度大致相同。


I have two text files containing the following:

FILE1.txt

dog
cat
antelope

FILE2.txt

1
2
Barry

The output I want to achieve is as follows:

dog1
dog2
dogBarry
cat1
cat2
catBarry
antelope1
antelope2
antelopeBarry

They way I have gone about it:

    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; 

The way I have done it works, but I'm sure it is not the best way of going about it especially when the files could both contain thousands of lines?

What would the best way of doing this be, to maybe use a hash?

解决方案

Your approach will work fine for files with thousands of lines. That really isn't that big. For millions of lines, it might be a problem.

However, you could reduce the memory usage of your code by only reading one file into memory, as well as printing the results immediately instead of 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>;   #each line of the file into an array
close $payloads or die "Can't close payloads: $!";

while (my $line = <$animals>) {
    chomp $line;
    print $line.$_ foreach (@payloads);
}
close $animals or die "Can't close animals: $!";

With two huge files of equal size, this will use roughly 1/4 the memory of your original code.

Update: I also edited the code to include Simbabque's good suggestions for modernizing it.

Update 2: As others have noted, you could read neither file into memory, going through the payloads file line by line on each line of the animals file. However, that would be much slower. It should be avoided unless absolutely necessary. The approach I have suggested will be about the same speed as your original code.

这篇关于Perl遍历文件中的每一行,并将其附加到另一个文件中每行的末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆