比较4个阵列的最佳方法

时间:2013-04-02 02:23:35

标签: perl perl-module

我有4个文件,每个文件都读入它自己的数组。然后我从那里将文件A,B和C相互比较。在每个步骤中,我打印出一个缺失的数字列表。

将A与B进行比较,然后将B与A进行比较 打印在A而不是B中找到的文件列表,反之亦然

将A与C进行比较,然后将C与A进行比较 打印在A中找到但不在C中的文件列表,反之亦然

将B与C和C与B进行比较 打印在B中找到但不在C中的文件列表,反之亦然

然后我必须从这些比较中获取值并将它们与文件D进行比较 仅打印文件D中找不到的文件

这是我到目前为止的代码。我只是认为可以有更好的方法来做到这一点,我想对此有所帮助。

[CODE]

sub compare {

    my ($nf, $of, $inf, $infw) = @_;

    open NF, $nf or die $!;
    my @note_file = <NF>;
    close NF;

    open OF, $of  or die $!;
    my @order_file = <OF>;
    close OF;

    open INF, $inf or die $!;
    my @invoice_file = <INF>;
    close INF;

    open INFW, $infw or die $!;
    my @invoicefw_file = <INFW>;
    close INFW;

    my $lc1 = List::Compare->new(\@note_file,\@order_file); 
    my @unique_in_note_file = $lc1->get_unique;
    my @unique_in_order_file = $lc1->get_complement;
    print "The following files exist only in the Brighton-Note file and not in the Brighton-Order file : " . "\n\n" . join("\n", @unique_in_note_file) . "\n" if(scalar(@unique_in_note_file) > 0);
    print "The following files exist only in the Brighton-Order file and not in the Brighton-Note file : " . "\n\n" . join("\n", @unique_in_order_file) . "\n" if(scalar(@unique_in_order_file) > 0);

    my $lc2 = List::Compare->new(\@note_file,\@invoice_file);
    @unique_in_note_file = $lc2->get_unique;
    my @unique_in_invoice_file = $lc2->get_complement;
    print "The following files exist only in the Brighton-Note file and not in the Web-Sales file : " . "\n\n" . join("\n", @unique_in_note_file) . "\n" if(scalar(@unique_in_note_file) > 0);
    print "The following files exist only in the Web-Sales file  and not in th Brighton-Note file : " . "\n\n" . join("\n", @unique_in_invoice_file) . "\n" if(scalar(@unique_in_invoice_file) > 0);

    my $lc3 = List::Compare->new(\@order_file, \@invoice_file);
    @unique_in_order_file = $lc3->get_unique;
    @unique_in_invoice_file = $lc3->get_complement;
    print "The following files exist only in the Brighton-Order file and not in the Web-Sales file : " . "\n\n" . join("\n", @unique_in_order_file) . "\n" if(scalar(@unique_in_order_file) > 0);
    print "The following files exist only in the Web-Sales file  and not in th Brighton-Order file : " . "\n\n" . join("\n", @unique_in_invoice_file) . "\n" if(scalar(@unique_in_invoice_file) > 0);

    my $lc4 = List::Compare->new(\@unique_in_note_file,\@invoicefw_file);
    my @unique_in_notefw_file = $lc4->get_unique;
    my @unique_in_invoicefw_file = $lc4->get_complement;
    print "The following files exist only in the Brighton-Note file and not in the Web-SalesFW file : " . "\n\n" . join("\n", @unique_in_notefw_file) . "\n" if(scalar(@unique_in_notefw_file) > 0);
    print "The following files exist only in the Web-SalesFW file  and not in th Brighton-Note file : " . "\n\n" . join("\n", @unique_in_invoicefw_file) . "\n" if(scalar(@unique_in_invoicefw_file) > 0);

    my $lc5 = List::Compare->new(\@unique_in_order_file,\@invoicefw_file);
    my @unique_in_orderfw_file = $lc5->get_unique;
    @unique_in_invoicefw_file = $lc5->get_complement;
    print "The following files exist only in the Brighton-Order file and not in the Web-SalesFW file : " . "\n\n" . join("\n", @unique_in_orderfw_file) . "\n" if(scalar(@unique_in_orderfw_file) > 0);
    print "The following files exist only in the Web-SalesFW file  and not in th Brighton-Order file : " . "\n\n" . join("\n", @unique_in_invoicefw_file) . "\n" if(scalar(@unique_in_invoicefw_file) > 0);

}

1 个答案:

答案 0 :(得分:4)

使用两个文件执行此操作的典型方法是:

my %items;
while (<$file1>) {
  $items{$_}++;
}
while (<$file2>) {
  $items{$_}++;
}

%items中任何值为2的密钥都在两个文件中;任何值为1的键只在一个文件中。

如果您需要知道每个值出现在哪个文件中,可以通过为每个文件添加不同的数字来概括。例如,如果为第一个文件中的行添加100,为第二个文件中的行添加10,为第三个文件中的行添加1,则可以立即看到值为101的键表示第一个文件中的行和第三个文件,但不在第二个文件中。