How many different ways are there to concatenate two files line by line using Perl?

前端 未结 5 2073
鱼传尺愫
鱼传尺愫 2020-12-15 13:59

Suppose file1 looks like this:

bye bye
hello
thank you

And file2 looks like this:

chao
hola
gracias

The desired output is this:

5条回答
  •  没有蜡笔的小新
    2020-12-15 14:35

    This works for any number of files:

    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;
    

提交回复
热议问题