Thanks a lot guys for your valuable comments.
Now that I have read the files (to be compared for uniqueness)into hashes and pushing the hashes into an array, I am trying to compare the uniqueness of the hashes. For that I have done the following:
use Data::Compare;
use Data::Dumper;
my $i = 0;
my $y = 0;
my $flag;
my $x = 1;
my @aoh;
my @dupAoh;
print "Tell me dude/dudette where are the log files: " , " \n";
$Location1 = <STDIN>;
chomp $Location1;
#print "And where would you like to put the processed results: " , "\n
+";
#$Location2 = <STDIN>;
#chomp $Location2;
$UserInputDir = $Location1;
opendir DH, $UserInputDir or die "Cannot open $UserInputDir: $!";
@files = readdir DH;
closedir DH;
foreach $file (@files) {
next if $file=~/^\./;
print "creating hash for $file....\n";
%filehash1 = filetohash($Location1.'\\'.$file);
push @aoh, \%filehash1;
#print Dumper(\\%filehash1);
$x++;
}
print scalar(@aoh)."\n";
@dupAoh = @aoh;
#print Dumper(\@dupAoh);
foreach $val (@aoh) {
#print "$val and $val++";
%hashValue1 = pop(@dupAoh);
for ($i = 0; $i < scalar(@dupAoh); ++$i){
%hashValue2 = @aoh[i];
#print Dumper(\\%hashValue1);
#print Dumper(\\%hashValue2);
Compare(\%hashValue1, \%hashValue2) ? "identical \n" : "not identi
+cal.\n";
#compHash(\%hashValue1, \%hashValue2);
}
}
sub compHash{
my %filehash1 = shift;
my %filehash2 = shift;
for $key (keys %filehash1) {
if($filehash2{$key} == 1){$flag = 1;}else{$flag = 0; last;}
+
print "Flag value is $flag \n";
}
($flag == 1) ? print "Yooohoo exactly same \n" : print "Not Same Alas!
+!!\n";
}
sub filetohash {
my $counter;
my $file = shift;
my %filehash;
open(FILE, "< $file") || die $!;
while (<FILE>) {
chomp $_;
s/^ *//;
s/ *$//;
$filehash{$_} = 1;
$counter++;
}
print "counted $counter lines for file $dir\n";
return \%filehash;
}
I am trying with one of my own functions and have tried data::compare module as well, about which I didn't get much time to play with.
I have copied the array of hashes into another array. From the duplicate array I am popping the hashes (in turn the size of array reducing with each pass) and comparing it with the second hash element onwards of the original array. Basically, for a give 10 hashes I want to compare 1st hash with subsequent 9 hashes and then in next pass 2nd hash with subsequent 8 hashes. Please let me know what wrong I am doing or my logic is skewed.
Thanks,
Xhings
|