Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^4: File:Find pattern match question

by RockE (Novice)
on Nov 01, 2013 at 01:00 UTC ( [id://1060673]=note: print w/replies, xml ) Need Help??


in reply to Re^3: File:Find pattern match question
in thread File:Find pattern match question

Hi Ken I tried your example but it wouldn't output anything. If I removed the pattern match string it does work but I'm not sure what it is printing out - the directory listing seems random.

#!/usr/bin/perl # dirpathdupes use strict; use warnings; use File::Find; use Fcntl; #*****************Path Variables********************** our $wellpath = 'N:\\repos\\open\\Wells\\Regulated\\'; our $surveypath = 'N:\\repos\\open\\Surveys\\Regulated\\'; our $testpath = 'C:\\Temp\\'; #******************************************************* my %dirs; find(\&dir_names, $testpath); my @dup_dirs = grep { $dirs{$_} > 1 } keys %dirs; #print "$_\n" for sort keys %dirs; foreach my $l (@dup_dirs) { print "$l\n"; } sub dir_names { # skip over everything that is not a directory return unless -d $File::Find::name; # skip over directories that don't match required pattern return unless $File::Find::dir =~ /[IPD]\d{8}$/; ++$dirs{$File::Find::dir}; }

Replies are listed 'Best First'.
Re^5: File:Find pattern match question
by kcott (Archbishop) on Nov 01, 2013 at 03:31 UTC
    "Hi Ken I tried your example but it wouldn't output anything."

    The technique I showed should work fine. In his response below, Athanasius has highlighted the issue with your original premise (i.e. parent vs. current directory). You can still use the technique I provided, you'll just need to work it into the code fix he's shown.

    "If I removed the pattern match string it does work but I'm not sure what it is printing out - the directory listing seems random."

    Hashes are unordered: keys %hash_name will return a list of keys in an apparently random order. If you're interested, see the "Hash Algorithm" section of "perlsec: Algorithmic Complexity Attacks" for more details.

    sort may provide the ordering you want. If not, you may want to consider an array, or perhaps a more complex data structure, instead of a hash, to store your data. See "perldsc - Perl Data Structures Cookbook".

    -- Ken

      Thanks again Ken, I have the following code, I do need to work on it a bit, at the moment it's printing out C:\Temp and nothing else. Athanasius code is printing out each of the P00 and I00 directories I made under C:\Temp for testing. I'll check out your example further, thanks

      #!/usr/bin/perl # dirpathdupes use strict; use warnings; use File::Find; use Fcntl; #*****************Path Variables********************** our $wellpath = 'N:\\repos\\open\\Wells\\Regulated\\'; our $surveypath = 'N:\\repos\\open\\Surveys\\Regulated\\'; our $testpath = 'C:\\Temp\\'; #******************************************************* my %dirs; find(\&dir_names, $testpath); my @dup_dirs = grep { $dirs{$_} > 1 } keys %dirs; foreach my $l (@dup_dirs) { print "$l\n"; } sub dir_names { return unless -d; return unless /[IPD]\d{8}$/; ++$dirs{$File::Find::dir};

        You originally had this to flag occurences:

        $dirs{$File::Find::dir} = 1;

        and this to count occurences:

        ++$dirs{$File::Find::dir};

        Because you weren't flagging the right thing, the code for that (which, as you say below, "Works perfectly! :-) Thank you very much! Cheers"), has been changed to:

        $dirs{$File::Find::name} = 1;

        Can you work out what you need to do so that you'd now be counting the right thing?

        -- Ken

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (6)
As of 2024-04-23 15:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found