0

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

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

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

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

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

这是我到目前为止的代码。我只是认为可能有更好的方法来做到这一点,我希望得到一些帮助。

[代码]

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);

}
4

1 回答 1

4

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

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

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

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

于 2013-04-02T05:51:44.040 回答