尝试输出两个文件的所有可能组合

时间:2013-03-10 20:29:54

标签: file compare

您好我正在尝试创建一个perl脚本,该脚本将包含2个文件(包含单词)并为每个可能的组合加入它们。

例如。

文件1包含:

  make 
  move

文件2包含:

   on
   out

输出文件应为:

   makeon
   makeout
   moveon
   moveout

理想情况下,将文件的订单改为I.E

会很好

onmove    outmove    onmake    outmake

以下是我尝试过的一些事情,但我是初学者,永远无法正确输出

! /usr/bin/perl



open (OUT, '> output.txt');

use strict;

use warnings;

use Data::Dump qw(dump);

my @in = qw(aaa bbb ccc ddd);

my @list;

while(my $first = shift @in) {

        last unless @in;
        my $rest = join'\n', @in;
        push @list, glob("{$first}{$rest}");
}
        dump @list;




# Some other attempts


my @karray;

my @sarray;

my @testarr = (@sarray)+@karray;

my $stemplate = "test1.txt";

my $ktemplate = "test2.txt";

my $outtemplate = "output.txt";

sub pushf2a {

  open(IN, "<$_[0]") || die;

  while (<IN>) {

    if ($_[0] eq $stemplate) {

      push (@sarray,@karray,$_);

    } else {

      push (@karray,@sarray,$_);
    }

  }

  close(IN) || die  $!;

}

&pushf2a($stemplate, @sarray);

&pushf2a($ktemplate, @karray);

print OUT @sarray, @karray;

print OUT @list;

close (OUT);

1 个答案:

答案 0 :(得分:0)

可能有更简单的方法,但这很简单:

#!/usr/bin/env perl
use Tie::File;
use strict;
use warnings;

tie my @a, 'Tie::File', $ARGV[0] or die "$ARGV[0]: $!\n";
tie my @b, 'Tie::File', $ARGV[1] or die "$ARGV[1]: $!\n";
foreach my $w( @b ) { print "$w$_\n" foreach( @a )}

使用您的两个输入文件作为参数调用此脚本。例如,如果将上面的内容放在名为“combine”的文件中并使其可执行:

./combine file1.txt file2.txt