我正在尝试比较两个输入文件中的maplayer / deflayer语句。我的目标是找到具有不同图层编号和数据类型的地图图层。同时打印,在一个文件中不存在但在另一个文件中存在的语句。这是示例代码,我想到了但是在哈希值比较中苦苦挣扎。请建议一个更好的方法来处理它。
#!/usr/bin/perl
use strict;
use Data::Dumper;
my $tmp1=$ARGV[0];
my $tmp2=$ARGV[1];
open(FILE1, $tmp1) or die;
open(FILE2, $tmp2) or die;
my @XY=();
my @XY2=();
my %hash1={};
my %hash2={};
my %hash3={};
my %hash4={};
my %hash5={};
my %hash6={};
while(<FILE1>) {
if (m/DEFLAYER.+/gi || m/MAPLAYER.+/gi ) {
push(@XY, $_);
}
}
while(<FILE2>) {
if (m/DEFLAYER.+/gi || m/MAPLAYER.+/gi ) {
push(@XY2, $_);
}
}
foreach(@XY) {
chomp;
if ( /deflayer\s+(\S+)\s+(\S+).*/gi ) {
$hash1{$2}=$1;
} elsif( /maplayer\s+([^-]+)-([^\s+]+)\s+-([^\s+]+)\s+(\d+)\s+(\S+)(.*)/gi) {
$hash2{$1}{$4}=$5;
} elsif( /maplayer\s+([^-]+)-([^\s+]+)\s+([^\s+]+)\s+(\d+)(.*)/gi) {
$hash2{$1}{$3}=$4;
}
}
foreach my $p1 (keys %hash1 ) {
foreach my $val1 (keys %hash2 ) {
foreach my $val2 (keys %{$hash2{$val1}} ) {
if ( $p1 == $hash2{$val1}{$val2} ) {
$hash3{$val1}{$val2}=$hash1{$p1};
}
}
}
}
foreach(@XY2) {
chomp;
if ( /deflayer\s+(\S+)\s+(\S+).*/gi ) {
$hash4{$2}=$1;
} elsif( /maplayer\s+([^-]+)-([^\s+]+)\s+-([^\s+]+)\s+(\d+)\s+(\S+)(.*)/gi) {
$hash5{$1}{$4}=$5;
} elsif( /maplayer\s+([^-]+)-([^\s+]+)\s+([^\s+]+)\s+(\d+)(.*)/gi) {
$hash5{$1}{$3}=$4;
}
}
foreach my $p1 (keys %hash4 ) {
foreach my $val1 (keys %hash5 ) {
foreach my $val2 (keys %{$hash5{$val1}} ) {
if ( $p1 == $hash5{$val1}{$val2} ) {
$hash6{$val1}{$val2}=$hash4{$p1};
}
}
}
}
foreach my $p1 (keys %hash3 ) {
foreach my $p2 (keys % { $hash3{$p1}} ) {
foreach my $tp1 (keys %hash6 ) {
foreach my $tp2 (keys % { $hash6{$tp1}} ) {
if ( $hash3{$p1}{$p2} ne $hash6{$tp1}{$tp2} ) {
print "$hash3{$p1}{$p2} is different from $hash6{$tp1}{$tp2}\n";
}
}
}
}
}
输入文件1:
DEFLAYER ABC 1001
MAPLAYER 10 DTYPE -eq 1 1001
DEFLAYER BC 1002
MAPLAYER 11 DTYPE -eq 2 1002
deflayer c 1003
MAPLAYER 11 -DTYPE 0 1003
deflayer d 1004
MAPLAYER 1 -DTYPE 5 1004;
CDE= CUSTOM AB
InputFile2:
DEFLAYER AC 1001
MAPLAYER 11 DTYPE -eq 1 1001
DEFLAYER BC 1002
MAPLAYER 11 DTYPE -eq 2 1002
deflayer c 1003
MAPLAYER 11 -DTYPE 0 1003
deflayer e 1004
MAPLAYER 1 -DTYPE 5 1004;
CDE= CUSTOM AB AC
预期产出:
deflayer d exist in file1 but missing in file2
deflayer e exiss in file2 but missing in file1
Print name of keys which are different
also print layer whose layer number and datatype are different.
问候,Div