I am pretty new to Perl and need to accomplish a task quickly. Any help is appreciated!
I have two hash of arrays as follows:
Hash 1
-------
abc.txt: ['0744','0']
xyz.txt: ['0744','0']
Hash 2
-------
abc.txt: ['0766','0']
x.txt: ['0744','0']
I have to compare these 2 hashes print 3 things:
1. Files Added in Hash2
2. Files Missing in Hash2
3. Files(keys) which are present in both hashes but there attributes(values) are different.
print "-------------------------ADDED FILES--------------------------------";
foreach (keys %hash2){
print "added $_\n" unless exists $hash1{$_};
}
print "-------------------------MISSING FILES--------------------------------";
foreach (keys %hash1){
print "Missing $_\n" unless exists $hash2{$_};
}
print "-------------------------Different permissions--------------------------------";
foreach my $key2 ( keys %hash2 ) {
unless ( exists $hash1{$key2} ) { next; };
if (join(",", sort @{ $hash1{$_}})
eq join(",", sort @{ $hash2{$_}}) ){
}
else{
print "value is different";
}
}
Issue is when keys are same.This for each loop doesn't work well.I want to print like this:
FileName: File Attributes Before : File Attributes after
abc.txt: '0744','0': 0766','0'
Please help
print "$key2: ".join(",", sort @{ $hash1{$key2}}.": ".join(",", sort @{ $hash2{$key2}}."\n"... or do you have a different question? - user3112922$hash1{$key2} ~~ $hash2{$key2}: ) See perldoc.perl.org/perlop.html#Smartmatch-Operator ... And then just print themn by usingprint "$key2: ".join(",", @{ $hash1{$key2}}).": ".join(",", @{ $hash2{$key2}})."\n"- user3112922