Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re^2: Comparing Values PER Sub-folder

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

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

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"; }

Replies are listed 'Best First'.
Re^3: Comparing Values PER Sub-folder
by Kenosis (Priest) on Sep 05, 2012 at 00:21 UTC

    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.

        Yes, provided that the top dir contains *.txt files. You can try commenting out /\.txt$/ or return;, so all files are displayed. Another option is to initialize $dir with the complete path to the 'top' directory you want File::Find to traverse, in case '.' may not be working.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://991710]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2018-06-24 02:01 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.