Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Comparing Values PER Sub-folder

by Kenosis (Priest)
on Sep 04, 2012 at 23:26 UTC ( #991702=note: print w/ replies, xml ) Need Help??


in reply to Comparing Values PER Sub-folder

If I correctly understand your goal, perhaps File::Find and File::Slurp will be helpful:

use Modern::Perl; use File::Find; use File::Slurp qw/read_file/; my $startDir = '.'; find( { wanted => \&countLines, }, $startDir ); 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; say "Cur dir: $curDir; Cur file: $curFile; Num Lines: $numLines"; }

Partial output:

Cur dir: ./test/test bbb; Cur file: B.txt; Num Lines: 6

The script above will start at $startDir and descent into directories, processing only *.txt files. Consider using a hash (key = $curDir; val = totLines) to store totalFileLines per directory.

Hope this helps!


Comment on Re: Comparing Values PER Sub-folder
Select or Download Code
Re^2: Comparing Values PER Sub-folder
by omegaweaponZ (Beadle) on Sep 05, 2012 at 00:07 UTC
    I think I see your logic, I can probably also just do an if/else to see if curdir matches to compare and contrast. I slightly modified the code to do this, but I'm getting no returns. Where am I going wrong? $dir is current working directory
    find(\&countLines, $dir); sub countLines { /\.txt$/ or return; my $completePath = $File::Find::name; my $curDir = $File::Find::dir; my $curFile = $_; tie my @filelines, 'Tie::File', $curFile or die; my $numLines = @filelines; print "Cur dir: $curDir; Cur file: $curFile; Num Lines: $numLines +\n"; }

      Let me first address another issue... Tie::File can be rather slow--especially when used on large files. This is why I used File::Slurp for the line count. Also, remember that you should untie the formerly tied array when done with it.

      I ran your subroutine, adding untie @filelines; before the end of the code block, and it executed just fine (it ran fine w/o that addition, too, but it's best to follow a tie with an untie). I'm unsure why you're not getting any output from the routine...

        Good call about untie. But even if I use slurp I'm still not recieving any print return results.
        find(\&countLines, $dir); sub countLines { /\.txt$/ or return; my $completePath = $File::Find::name; my $curDir = $File::Find::dir; my $curFile = $_; my @lines = read_file( $curFile ) ; my $numLines = @lines; print "Cur dir: $curDir; Cur file: $curFile; Num Lines: $numLines +\n";

        Is this definitely finding every .txt file in ALL sub-directories? I don't understand why it wouldn't even print a current directory or current file unless that .txt parameter is not working and its finding 0 files.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2014-08-30 20:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (293 votes), past polls