The following pieces together the script segments:

use strict; use warnings; use File::Find; use File::Slurp qw/read_file/; my $startDir = '.'; my %dirLines; find( { wanted => \&countLines, }, $startDir ); for my $dir ( sort keys %dirLines ) { my $sameResults = sameArrayElements( @{ $dirLines{$dir} } ); print "The files in directory '$dir' do" . ( $sameResults ? '' : ' not' ) . " have the same number of lines.\n"; } sub countLines { /\.txt$/ or return; #my $completePath = $File::Find::name; my $curDir = $File::Find::dir; my $curFile = $_; my @fileLines = read_file $curFile; my $numLines = @fileLines; push @{ $dirLines{$curDir} }, $numLines; #say "Cur dir: $curDir; Cur file: $curFile; Num Lines: $numLines"; } sub sameArrayElements { my %hash = map { $_ => 1 } @_; keys %hash == 1 ? 1 : 0; }

Notice that we now have my %dirLines; at the top. We'll use this hash for a hash of arrays (HoA), where the key will be the directory path and the associated value is an array whose elements are file numLines.

The following was added to the countLines subroutine:

push @{ $dirLines{$curDir} }, $numLines;

$dirLines{$curDir} is our hash and the value of $curDir is used as a key. The enclosing @{ } notation says to treate this as an array (we'll see this later, too), and then we push the value of $numLines onto that array.

Data::Dumper was used to help visualize our hash's data structure after traversing the directories:

$VAR1 = { './test/test bbb' => [ 6, 6, 6, 6 ], './test' => [ 6, 2, 6, 1 ], '.' => [ 6, 2, 6, 10000, 1, 63, 10, 15, 6, 21, 647, 10, 5, 28, 407, 2390, 11, 6, 513, 181, 1, 2, 360, 3 ] };

Curley braces {} mean a hash; square brackets [] mean an array. From the output above, you can see the association between a key (directory path) and an array (a list of file line numbers) as a value.

The next step is to process our hash, iterating through its keys--one at a time--and that starts as follows:

for my $dir ( sort keys %dirLines ) { my $sameResults = sameArrayElements( @{ $dirLines{$dir} } ); ...

Each sorted key of %dirLines is assigned to $dir. Next is the same notation seen earlier, viz., @{ $dirLines{$dir} }. The directory's associated array of numLines is sent to sameArrayElements to check for element sameness. The following line prints the result of this check:

The files in directory '.' do not have the same number of lines. The files in directory './test' do not have the same number of lines. The files in directory './test/test bbb' do have the same number of li +nes.

In reply to Re^11: Comparing Values PER Sub-folder by Kenosis
in thread Comparing Values PER Sub-folder by omegaweaponZ

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.