Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 字符串比较_String_Perl_Compare_String Comparison - Fatal编程技术网

String 字符串比较

String 字符串比较,string,perl,compare,string-comparison,String,Perl,Compare,String Comparison,我有两个从IntexCalc和Intex API生成的文件,我想比较它们的内容。我不想逐行比较。下面的例子将给出更多的细节 文件1 文件2 我想比较两个文件中的损耗\u单位[\“GRPY\”]标志值。在这两个文件中,无论它们在文件中的位置如何,它们在=/==之后的值都相同,因此此标志值相等 文件1中的LOSS\u USERCURVE\u INDEX\u OFFSET[\“GRPY\”]的标志值为BY\u LOAN\u AGE,文件2中的BY\u DEAL\u AGE,因此该标志值不同 标记丢

我有两个从IntexCalc和Intex API生成的文件,我想比较它们的内容。我不想逐行比较。下面的例子将给出更多的细节

文件1 文件2
  • 我想比较两个文件中的
    损耗\u单位[\“GRPY\”]
    标志值。在这两个文件中,无论它们在文件中的位置如何,它们在=/==之后的值都相同,因此此标志值相等

  • 文件1中的
    LOSS\u USERCURVE\u INDEX\u OFFSET[\“GRPY\”]
    的标志值为
    BY\u LOAN\u AGE
    ,文件2中的
    BY\u DEAL\u AGE
    ,因此该标志值不同

  • 标记
    丢失率[\“GRPY\”]
    仅出现在文件1中,因此这是一个区别

  • 标志
    丢失\u严重性[\“GRPY\”]
    仅存在于文件2中,因此这也是一个区别


  • 比较这种文件结构的最佳方法或工具是什么?

    一个没有灵感的解决方案:将键和值放入两个哈希中,并进行比较

    sub f2h {
      my( $hr, $path ) = @_;    
      open FILE, $path or die "$path: couldn't open: $!";
      while( my $line = <FILE> ){
        $line =~ s/\s+$//;   # there are trailing spaces in your data
        my( $key, $val ) = split( /==?/, $line );
        $hr->{$key} = $val;
      }
      close FILE;
    }
    
    my %h1;
    my %h2;
    f2h( \%h1, "file1.dat" );
    f2h( \%h2, "file2.dat" );
    while( my( $k, $v ) = each %h1 ){
      if( exists( $h2{$k} ) ){
        print "different $k\n" if $h2{$k} ne $v;
      } else {
        print "$k missing in 2\n";
      }
    }
    while( my( $k, $v ) = each %h2 ){
      print "$k missing in 1\n" unless exists $h1{$k};
    }
    
    sub-f2h{
    我的($hr,$path)=@;
    打开文件,$path或死亡“$path:无法打开:$!”;
    while(我的$line=){
    $line=~s/\s+$/;#数据中有尾随空格
    我的($key,$val)=拆分(/=?/,$line);
    $hr->{$key}=$val;
    }
    关闭文件;
    }
    我的%h1;
    我的%h2;
    f2h(\%h1,“file1.dat”);
    f2h(\%h2,“file2.dat”);
    而(我的($k,$v)=每个%h1){
    如果(存在($h2{$k})){
    如果$h2{$k}ne$v,则打印“不同的$k\n”;
    }否则{
    打印“$k在2中丢失\n”;
    }
    }
    而(我的($k,$v)=每个%h2){
    除非存在$h1{$k},否则打印“$k在1\n中丢失”;
    }
    
    我建议您使用该模块

    它返回对散列的引用,该散列包含参数之间差异的摘要。钥匙是

    • 相同
      -两种情况下相同的元素
    • diff
      -对于给定键具有不同值的元素
    • uniq_a
      uniq_b
      -仅出现在一个或另一个结构中的元素


    diff-u0@ysth这两个命令都很可爱,但是您需要压缩一些sed过滤器来消除由“=”和“=”引起的差异。而且报道也不完美。
    
    LOSS_USERCURVE_TYPE[\"GRPY\"]=PCT_MULTIPLY  
    LOSS_NONPERF_ADV_PCT_P[\"GRPY\"]=0  
    LOSS_UNITS[\"GRPY\"]=CDR  
    LOSS_NONPERF_ADV_PCT_I[\"GRPY\"]=0  
    SEVERITY_USERCURVE_TYPE[\"GRPY\"]=NONE  
    LOSS_SEVERITY[\"GRPY\"]=31.73  
    LOSS_USERCURVE_INDEX_OFFSET[\"GRPY\"]=BY_DEAL_AGE  
    
    sub f2h {
      my( $hr, $path ) = @_;    
      open FILE, $path or die "$path: couldn't open: $!";
      while( my $line = <FILE> ){
        $line =~ s/\s+$//;   # there are trailing spaces in your data
        my( $key, $val ) = split( /==?/, $line );
        $hr->{$key} = $val;
      }
      close FILE;
    }
    
    my %h1;
    my %h2;
    f2h( \%h1, "file1.dat" );
    f2h( \%h2, "file2.dat" );
    while( my( $k, $v ) = each %h1 ){
      if( exists( $h2{$k} ) ){
        print "different $k\n" if $h2{$k} ne $v;
      } else {
        print "$k missing in 2\n";
      }
    }
    while( my( $k, $v ) = each %h2 ){
      print "$k missing in 1\n" unless exists $h1{$k};
    }
    
    use strict;
    use warnings 'all';
    use autodie;
    
    use Data::Dump;
    use Data::Diff 'Diff';
    
    my %f1 = do {
        open my $fh, '<', 'file1.txt';
        map { s/\s+\z//; split /=+/, $_, 2 } <$fh>;
    };
    
    my %f2 = do {
        open my $fh, '<', 'file2.txt';
        map { s/\s+\z//; split /=+/, $_, 2 } <$fh>;
    };
    
    my $diff = Diff(\(%f1, %f2));
    dd $diff;
    
    {
      diff   => {
                  "LOSS_USERCURVE_INDEX_OFFSET[\\\"GRPY\\\"]" => { diff_a => "BY_LOAN_AGE", diff_b => "BY_DEAL_AGE", type => "" },
                },
      same   => {
                  "LOSS_NONPERF_ADV_PCT_I[\\\"GRPY\\\"]"  => { same => 0, type => "" },
                  "LOSS_NONPERF_ADV_PCT_P[\\\"GRPY\\\"]"  => { same => 0, type => "" },
                  "LOSS_UNITS[\\\"GRPY\\\"]"              => { same => "CDR", type => "" },
                  "LOSS_USERCURVE_TYPE[\\\"GRPY\\\"]"     => { same => "PCT_MULTIPLY", type => "" },
                  "SEVERITY_USERCURVE_TYPE[\\\"GRPY\\\"]" => { same => "NONE", type => "" },
                },
      type   => "HASH",
      uniq_a => { "LOSS_RATE[\\\"GRPY\\\"]" => 100 },
      uniq_b => { "LOSS_SEVERITY[\\\"GRPY\\\"]" => 31.73 },
    }