Chinaunix首页 | 论坛 | 博客
  • 博客访问: 226535
  • 博文数量: 50
  • 博客积分: 1415
  • 博客等级: 上尉
  • 技术积分: 541
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-03 11:48
文章分类

全部博文(50)

文章存档

2010年(38)

2009年(12)

我的朋友

分类:

2009-12-03 11:52:18


 

#!/usr/local/bin/perl -w

use strict;
use File::Find;
my $path = ".";

###目录下的文件打包到下面这个数组

my @mydir;
###要对比的两个文件

my ($fileOne,$fileTwo);
###存1号文件用的数组

my %mingzhi;

####读取目录文件

find(\&process, $path);

sub process {
        if($_ =~ /(\.txt$)/){
         #print $_."\n";

         push (@mydir,$_);
        }
   }
#print join "\n",@mydir;

#print "\n";

my $i = 1;
foreach my $myfile (@mydir){
print $i." : ".$myfile."\n";
$i++;
}

&enterthenum;

if (($fileOne)and($fileTwo)) {
&suchu ($fileOne,$fileTwo);
};




print "对比结束,祝你愉快";
if (<STDIN>){};



#################下面全是子函数


#########提示用户输入

sub enterthenum {
     print "请猪选择要对比的文件"."\n";
     chomp(my $x= <STDIN>);
     #my ($z,$y) = $x =~ /(\d+)\s+(\d+)/;

      if ($x =~ /(\d+)\s+(\d+)/){
            ($fileOne,$fileTwo) = ($1,$2);
            print "您输入的是:"."$fileOne"."号"." 和"." $fileTwo"."号"."文件\n";
      } else {
            print "你有病啊?乱选!填数字喔"."\n";
           }
}



sub suchu {
    my ($agr1,$agr2) = @_;
    my (%agrhash1,%agrhash2);
    
    %agrhash1 = &inputhash($mydir[($agr1-1)]);
    my @paixu = &array(\%agrhash1);
    &write(\@paixu,($mydir[($agr1-1)]."顺序表"));
    
    %agrhash2 = &inputhash($mydir[($agr2-1)]);
    my @paixu2 = &array(\%agrhash2);
    &write(\@paixu2,($mydir[($agr2-1)]."顺序表"));
    my @array = &thedifference(\%agrhash1,\%agrhash2);
    &jieguo(@array);
    
    my %toushia = &findtoushi(\%agrhash1);
    my %toushib = &findtoushi(\%agrhash2);
    my @arraytoushi = &thedifference(\%toushia,\%toushib);
    &jieguotoushi(@arraytoushi);
    
    #my ($ohave,$thave,$diffe) = @array;

    #&write(\@{$ohave},($mydir[($agr1-1)]."独有"));

    #&write(\@{$thave},($mydir[($agr2-1)]."独有"));

    #&write(\@{$diffe},("两者共有"));

    
    #&write(\@{$array[0]},($mydir[($agr1-1)]."独有"));

    #&write(\@{$array[1]},($mydir[($agr2-1)]."独有"));

}



sub jieguo {
    my ($ohave,$thave,$diffe) = @_;
    &write(\@{$ohave},("A独有"));
    &write(\@{$thave},("B独有"));
    &write(\@{$diffe},("两者共有"));
}

sub jieguotoushi {
    my ($ohave,$thave,$diffe) = @_;
    &write(\@{$ohave},("A独有头饰"));
    &write(\@{$thave},("B独有头饰"));
    &write(\@{$diffe},("两者共有头饰"));
}

sub findtoushi {
   my ($toushi) = @_;
   my %have;
   foreach my $weibiao (keys %{$toushi}){
   if(($weibiao >= 5000) and $weibiao <= 6000){
   $have{"$weibiao"} = ${$toushi}{$weibiao};
   }
  }
  return %have;
}

######读取文本文件返回hash

sub inputhash {
    my ($linshi) = @_;
    my %hash;
    open(FILE, "<$linshi") or die "文件打开失败 ";
    while(<FILE>){
    if (/^(\d+)\#([\x80-\xff]+)\#/) {

    my $x=$1;
    my $y=$2;
    #print $x."=>".$y."\n";

    $hash{"$x"} = $y;
    #push(%mingzhi,($x => $y));

       }
    }
   close FILE;
   return %hash;
}

#####清除原有文件,并把数组写入文件

sub write {
   my ($arry,$linshi) = @_;
   $linshi =~ s/\.txt//g;
   open(FILE, "+>$linshi.txt") or die "文件打开失败 ";            
    foreach my $hashref (@{$arry}) {
     #my($key, $value) = each %$hashref;

     my($key, $value) = %{$hashref};
     print FILE "$key\#$value\n";
     #print "$key\#$value\n";

    }
    close FILE;
}

###对hash数组按KEY进行排序 参数是一个hash引用

sub array {
   my ($hash) = @_;
   my @paixu = map { { ($_ => ${$hash}{$_}) } }
              sort {$a <=>$b} keys %{$hash};
  return @paixu;
}

sub thedifference {
   my ($onehash,$twohash) = @_;
   my %onehave;
   my %twohave;
   my %thrhave;
   my @fuck;
   ######找出1有2没有的

   foreach my $weibiao (keys %{$onehash}){
   if(!(${$twohash}{$weibiao})) {
     $onehave{"$weibiao"} = ${$onehash}{$weibiao};
    }
    }
   ######找出2有1没有的

   foreach my $weibiao (keys %{$twohash}){
   if(!(${$onehash}{$weibiao})) {
   $twohave{"$weibiao"} = ${$twohash}{$weibiao};
   }
  }
  ######找出两者共有的

  foreach my $weibiao (keys %{$twohash}){
   if(${$onehash}{$weibiao}){
   $thrhave{"$weibiao"} = ${$onehash}{$weibiao}."#".${$twohash}{$weibiao};
   }
  }
  
  my @fuck1 = &array(\%onehave);
  my @fuck2 = &array(\%twohave);
  my @fuck3 = &array(\%thrhave);
  @fuck = (\@fuck1,\@fuck2,\@fuck3);
  return @fuck;
}


阅读(958) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:perl文本对比源码(原创)(改进版)

给主人留下些什么吧!~~