假设file1如下所示:
bye bye hello thank you
file2看起来像这样:
chao hola gracias
所需的输出是:
bye bye chao hello hola thank you gracias
我自己已经提出了五种不同的方法来解决这个问题。但我认为必须有更多的方法,可能更简洁,更优雅的方式,我希望我能学到更多很酷的东西:)
以下是我迄今为止所尝试的内容,基于我从以前的问题的许多解决方案中学到的东西。此外,我正在尝试消化或内化我从Llama书中获得的知识。
代码1:
#!perl
use autodie;
use warnings;
use strict;
open my $file1,'<','c:/file1.txt';
open my $file2,'<','c:/file2.txt';
while(defined(my $line1 = <$file1>)
and defined(my $line2 = <$file2>)){
die "Files are different sizes!\n" unless eof(file1) == eof(file2);
$line1 .= $line2;
$line1 =~ s/\n/ /;
print "$line1 \n";
}
代码2:
#!perl
use autodie;
use warnings;
use strict;
open my $file1,'<','c:/file1.txt';
my @file1 = <$file1>;
open my $file2,'<','c:/file2.txt';
my @file2 =<$file2>;
for (my $n=0; $n<=$#file1; $n++) {
$file1[$n] .=$file2[$n];
$file1[$n]=~s/\n/ /;
print $file1[$n];
}
代码3:
#!perl
use autodie;
use warnings;
use strict;
open my $file1,'<','c:/file1.txt';
open my $file2,'<','c:/file2.txt';
my %hash;
while(defined(my $line1 = <$file1>)
and defined(my $line2 = <$file2>)) {
chomp $line1;
chomp $line2;
my ($key, $val) = ($line1,$line2);
$hash{$key} = $val;
}
print map { "$_ $hash{$_}\n" } sort keys %hash;
代码4:
#!perl
use autodie;
use warnings;
use strict;
open my $file1,'<','c:/file1.txt';
open my $file2,'<','c:/file2.txt';
while(defined(my $line1 = <$file1>)
and defined(my $line2 = <$file2>)) {
$line1 =~ s/(.+)/$1 $line2/;
print $line1;
}
代码5:
#!perl
use autodie;
use warnings;
use strict;
open my $file1,'<','c:/file1.txt';
my @file1 =<$file1>;
open my $file2,'<','c:/file2.txt';
my @file2 =<$file2>;
while ((@file1) && (@file2)){
my $m = shift (@file1);
chomp($m);
my $n = shift (@file2);
chomp($n);
$m .=" ".$n;
print "$m \n";
}
我尝试过这样的事情:
foreach $file1 (@file2) && foreach $file2 (@file2) {...}
但是Perl给了我一个句法错误警告。我很沮丧。但我们可以同时运行两个foreach
循环吗?
一如既往地感谢任何评论,建议以及慷慨的代码共享:)
答案 0 :(得分:12)
这适用于任意数量的文件:
use strict;
use warnings;
use autodie;
my @handles = map { open my $h, '<', $_; $h } @ARGV;
while (@handles){
@handles = grep { ! eof $_ } @handles;
my @lines = map { my $v = <$_>; chomp $v; $v } @handles;
print join(' ', @lines), "\n";
}
close $_ for @handles;
答案 1 :(得分:10)
最优雅的方式根本不涉及perl
:
paste -d' ' file1 file2
答案 2 :(得分:7)
如果我是一名高尔夫球手,我可以将@FM's answer重写为:
($,,$\)=(' ',"\n");@_=@ARGV;open $_,$_ for @_;print
map{chomp($a=<$_>);$a} @_=grep{!eof $_} @_ while @_
你可能会变成一个单行,但这只是邪恶。 ; - )
嗯,这里是100个字符以下:
C:\Temp> perl -le "$,=' ';@_=@ARGV;open $_,$_ for @_;print map{chomp($a =<$_>);$a} @_=grep{!eof $_ }@_ while @_" file1 file2
如果可以啜饮(为什么不这样做 - 我们 寻找不同的方式),我想我已经发现了疯狂的道路:
@_=@ARGV;chomp($x[$.-1]{$ARGV}=$_) && eof
and $.=0 while<>;print "@$_{@_}\n" for @x
C:\Temp> perl -e "@_=@ARGV;chomp($x[$.-1]{$ARGV}=$_) && eof and $.=0 while<>;print qq{@$_{@_}\n} for @x" file1 file2
输出:
bye bye chao hello hola thank you gracias
答案 3 :(得分:2)
一个简单的错误检查:
#!/usr/bin/perl -w
use strict;
open FILE1, '<file1.txt';
open FILE2, '<file2.txt';
while (defined(my $one = <FILE1>) or defined(my $twotemp = <FILE2>)){
my $two = $twotemp ? $twotemp : <FILE2>;
chomp $one if ($one);
chomp $two if ($two);
print ''.($one ? "$one " : '').($two ? $two : '')."\n";
}
不,你不能在同一个线程中同时运行两个循环,你必须fork
,但不能保证同步运行。
答案 4 :(得分:2)
代码5 的更简单的替代方法,它允许任意数量的行,并且不关心文件是否有不同的行数(帽子提示@FM):
#!/usr/bin/perl
use strict; use warnings;
use File::Slurp;
use List::AllUtils qw( each_arrayref );
my @lines = map [ read_file $_ ], @ARGV;
my $it = each_arrayref @lines;
while ( my @lines = grep { defined and chomp and length } $it->() ) {
print join(' ', @lines), "\n";
}
并且,不使用任何外部模块:
#!perl
use autodie; use warnings; use strict;
my ($file1, $file2) = @ARGV;
open my $file1_h,'<', $file1;
my @file1 = grep { chomp; length } <$file1_h>;
open my $file2_h,'<', $file2;
my @file2 = grep { chomp; length } <$file2_h>;
my $n_lines = @file1 > @file2 ? @file1 : @file2;
for my $i (0 .. $n_lines - 1) {
my ($line1, $line2) = map {
defined $_ ? $_ : ''
} $file1[$i], $file2[$i];
print $line1, ' ', $line2, "\n";
}
如果只想连接两个文件中出现的行:
#!perl
use autodie; use warnings; use strict;
my ($file1, $file2) = @ARGV;
open my $file1_h,'<', $file1;
my @file1 = grep { chomp; length } <$file1_h>;
open my $file2_h,'<', $file2;
my @file2 = grep { chomp; length } <$file2_h>;
my $n_lines = @file1 < @file2 ? @file1 : @file2;
for my $i (0 .. $n_lines - 1) {
print $file1[$i], ' ', $file2[$i], "\n";
}