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

前端 未结 5 2056
鱼传尺愫
鱼传尺愫 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:19

    If I were a golfing man, I could rewrite @FM's answer as:

    ($,,$\)=(' ',"\n");@_=@ARGV;open $_,$_ for @_;print
    map{chomp($a=<$_>);$a} @_=grep{!eof $_} @_ while @_
    

    which you might be able to turn into a one-liner but that is just evil. ;-)

    Well, here it is, under 100 characters:

    C:\Temp> perl -le "$,=' ';@_=@ARGV;open $_,$_ for @_;print map{chomp($a =<$_>);$a} @_=grep{!eof $_ }@_ while @_" file1 file2

    If it is OK to slurp (and why the heck not — we are looking for different ways), I think I have discovered the path the insanity:

    @_=@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

    Output:

    bye bye chao
    hello hola
    thank you gracias
    

提交回复
热议问题