#!/usr/local/bin/perl -w
use strict; use File::Find; my $path = ".";
###目录下的文件打包到下面这个数组
my @mydir; ###要对比的两个文件 my ($fileOne,$fileTwo); ###存1号文件用的数组 ####读取目录文件 find(\&process, $path);
###把文本文件名写入数组中
sub process { if($_ =~ /(\.txt$)/){ #print $_."\n";
push (@mydir,$_); } }
####主程序块
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,["A独有","B独有","两者共有"]); my $copyarraytoushi = &thedifference($agrhash1,$agrhash2,"5000-6000"); &jieguo($copyarraytoushi,["A独有头饰","B独有头饰","两者共有头饰"]); }
#####接收两个参数,均为数组引用
sub jieguo { my ($neinong,$biaoti) = @_; for(my $i=0; $i <= 2;$i++){ &write(${$neinong}[$i],${$biaoti}[$i]); } }
sub searchhashkey { my ($toushi,$area) = @_; my %have; my ($x,$y); if (($area)and($area =~ /(\d+)\-(\d+)/)){ $x =$1; $y =$2; foreach my $weibiao (keys %{$toushi}){ if(($weibiao >= $x) and $weibiao <= $y){ $have{"$weibiao"} = ${$toushi}{$weibiao}; } } return \%have; }else{ return $toushi; }
}
######读取文本文件返回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,$area) = @_; my %onehave; my %twohave; my %thrhave; my @fuck; if ($area){ $onehash = &searchhashkey($onehash,$area); $twohash = &searchhashkey($twohash,$area); } ######找出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; }
|