Instead of having a hash for each directory, you could have a hash of hashes:

#!/opt/perl5/bin/perl -w use strict; use File::Slurp; # The production files will be listed in # the source hashes my $report = "/home/mis/tstanley/SourceDiff.rpt"; my %input = ( iqs_source => '/dsmpayroll/iqs-source', prg_source => '/dsmpayroll/prg-source', spg_source => '/dsmpayroll/spg-source', tpr_source => '/dsmpayroll/tpr-source', iqs => '/dsmmigrate/development/iqs', prg => '/dsmmigrate/development/prg', spg => '/dsmmigrate/development/spg', tpr => '/dsmmigrate/development/tpr', ); my %data; foreach my $key (keys(%input)) { my $dir = $input{$key}; chdir($dir); foreach my $file (read_dir('.')) { my $sum = `sum $file`; my ($x) = split(/\s+/, $sum); $data{$key}{$file} = $x; } } ## Open the report file ...

Update: It seems there's no need to build a hash of hashes, since we only need to look at a pair of directories at a time. I elminated the pesky chdir commands while I was at it. Here it is a version with (just about) all redundancy eliminated, but without any change in functionality:

#!/opt/perl5/bin/perl -w use strict; # The production files will be listed in # the source hashes my $report = "/home/mis/tstanley/SourceDiff.rpt"; sub DEV () { 0 } sub PROD () { 1 } my %input = ( IQS => [ '/dsmmigrate/development/iqs', '/dsmpayroll/iqs-source' ], PRG => [ '/dsmmigrate/development/prg', '/dsmpayroll/prg-source' ], SPG => [ '/dsmmigrate/development/spg', '/dsmpayroll/spg-source' ], TPR => [ '/dsmmigrate/development/tpr', '/dsmpayroll/tpr-source' ], ); ## Open the report file open RPT, ">$report" or die "Unable to open $report: $!\n"; my $date=`date`; print RPT "$date\n\n"; ## For each set of directories, we find the common file names in them, ## and then go through those files and compare the sums. If the ## Production file is different, we write it out to the report file. my $print_seperator; # False for first loop iteration. foreach my $key (sort keys %input) { local *DH; my $dir; my $file; my %dev_crcs; my %prod_crcs; $dir = $input{$key}[DEV]; opendir(DH, $dir) or die "Unable to open dir $dir: $!\n"; while (defined($file = readline(DIR))) { next if $_ eq '.'; next if $_ eq '..'; next if $_ eq 'cob'; next if substr($_, -4) eq '.ffl'; next if substr($_, -4) eq '.lst'; $dev_crcs{$file} = 0+`sum $dir/$file`; } $dir = $input{$key}[PROD]; opendir(DH, $dir) or die "Unable to open dir $dir: $!\n"; while (defined($file = readline(DIR))) { next if $_ eq '.'; next if $_ eq '..'; next if substr($_, -4) eq '.ffl'; next if substr($_, -4) eq '.lst'; $prod_crcs{$file} = 0+`sum $dir/$file`; } if ($print_seperator) { print RPT "\n\n"; } else { # True for all but first loop iteration. $print_seperator = 1; } print RPT "$key Source Files\n"; print RPT ("=" x length($key)) . "=============\n"; foreach $file (keys(%dev_crcs)) { if (exists($prod_crcs{$file}) && $prod_crcs{$file} != $dev_crcs{$file}) { print RPT "$dir/$file\n" } } }

Keep in mind that files in one directory but not in the other are not printed, just like in your solution.


In reply to Re: File Differences by ikegami
in thread File Differences by TStanley

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.