Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^6: Comparing Values PER Sub-folder

by omegaweaponZ (Beadle)
on Sep 05, 2012 at 02:19 UTC ( #991724=note: print w/ replies, xml ) Need Help??


in reply to Re^5: Comparing Values PER Sub-folder
in thread Comparing Values PER Sub-folder

Looks like I had some type of perl package mis-match. I was attempting to initialize File::Find::Rule on a prior test, which conflicted with something. I re-arranged something also in a different sub and it seems to be working just fine now. I think slurp is a good way to grab this, by setting these to a variable I think a simple comparison of directory to file lines should work well, but I will post again if something else seems to go astray. Thank you for your help!


Comment on Re^6: Comparing Values PER Sub-folder
Re^7: Comparing Values PER Sub-folder
by Kenosis (Priest) on Sep 05, 2012 at 02:29 UTC

    Great, omegaweaponZ--and you're very welcome!! Am glad you got it working!

      One more question!!!! Regarding the final component to this, I'm attempting to locate the best method to compare and contrast per directory, correct? So the print output would be something like this:

      Cur dir: /folder/blahA/blah1; Cur file: file.txt; Num Lines: 8
      Cur dir: /folder/blahA/blah2; Cur file: file.txt; Num Lines: 8
      Cur dir: /folder/blahA/blah3; Cur file: file.txt; Num Lines: 8
      Cur dir: /folder/blahA/blah4; Cur file: file.txt; Num Lines: 8
      Cur dir: /folder/blahA/blah5; Cur file: file.txt; Num Lines: 8
      Cur dir: /folder/blahA/blah6; Cur file: file.txt; Num Lines: 8

      Cur dir: /folder/blahB/blah1; Cur file: file.txt; Num Lines: 10
      Cur dir: /folder/blahB/blah2; Cur file: file.txt; Num Lines: 10
      Cur dir: /folder/blahB/blah3; Cur file: file.txt; Num Lines: 10
      Cur dir: /folder/blahB/blah4; Cur file: file.txt; Num Lines: 12
      Cur dir: /folder/blahB/blah5; Cur file: file.txt; Num Lines: 10
      Cur dir: /folder/blahB/blah6; Cur file: file.txt; Num Lines: 10

      So the offending folder of blahB has one text file that doesn't match up to the rest. How can I detect and alert on that? Basically, I want to set True if all num lines are equal to each other per directory and False if they don't

        Well, I don't know about "the best method," but here's one method:

        use Modern::Perl; my @same = (8) x 8; # an array of eight 8s my @notSame = qw/ 10 10 10 12 10 10 /; say "The array's elements are" . ( sameArrayElements(@same) ? '' : ' not' ) . ' the same.'; sub sameArrayElements { my %hash = map { $_ => 1 } @_; keys %hash == 1 ? 1 : 0; }

        If you send the subroutine sameArrayElements an array, it'll return 1 (true) if the array's elements are identical, and 0 (false) otherwise (even if an empty array is sent). As shown above, the output is:

        The array's elements are the same.

        Try sameArrayElements with @notSame.

        The subroutine uses map to iterate through all elements of the sent array, populating %hash with key/value pairs (the $_ => 1 notation). If the array elements are identical, there should be only one key--and the last line in the subroutine tests for that.

        Thus, if you pushed each numLines from a dir read onto an array, and then sent that array to sameArrayElements, it'll tell you whether all files in that dir have the same number of lines or not. (Be sure the array's empty, e.g, by using my @array;, before pushing values onto it.)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://991724]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (12)
As of 2014-12-26 14:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (171 votes), past polls