Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Finding un-paired files in a directory

by Anonymous Monk
on Dec 02, 2003 at 19:01 UTC ( #311714=note: print w/replies, xml ) Need Help??


in reply to Finding un-paired files in a directory

You want the "*.did" files that have no corresponding ".mrg" file, right?

Hmmm... let me type something in here...
use strict; use File::Find; my %temp; find( sub { s/mrg$/did/; /did$/ and $temp{$_}++; }, '.' ); my @files = grep { $temp{$_} == 1 } keys %temp; print "@files";
This code takes advantage of the fact that you will have only one or two files with the same basename (and that there are only two extensions of interest). So, just count them up.

It doesn't handle the case of a lone ".mrg" file. To fix that...
use strict; use File::Find; my %temp; find( sub { m/^(.+)\.(mrg|did)/ and push(@{$temp{"$1.did"}}, $2) }, '.' ); my @files = grep { @{ $temp{$_} } == 1 and $temp{$_}->[0] eq 'did' } sort keys %temp; print "@files";
The beauty of these code snippets (actually, they are working scripts) is that they don't do any filetests. File::Find (or readdir if you prefer) have already established the existence of the files -- checking for that again just slows down your code.

:)

-Dave

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2023-10-04 03:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?