http://qs321.pair.com?node_id=311714


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