Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Finding un-paired files in a directory

by Roger (Parson)
on Dec 02, 2003 at 12:10 UTC ( [id://311589]=note: print w/replies, xml ) Need Help??


in reply to Finding un-paired files in a directory

Here's my implementation -
use strict; use Data::Dumper; chomp(my @files = <DATA>); # find unique basenames my @basenames = keys %{{ map { (split/\./)[0] => 1 } @files }}; # find unpaired files foreach my $basename (@basenames) { my @matches = grep (/$basename/, @files); print "$matches[0]\n" if $#matches == 0; } __DATA__ 00000001.did 00000001.mrg 00000002.did 00000002.mrg 00000003.did 00000004.mrg
And the output -
00000004.mrg 00000003.did

Replies are listed 'Best First'.
•Re: Re: Finding un-paired files in a directory
by merlyn (Sage) on Dec 02, 2003 at 12:16 UTC
    use strict; use Data::Dumper; chomp(my @files = <DATA>); # find unique basenames my @basenames = keys %{{ map { (split/\./)[0] => 1 } @files }}; # find unpaired files foreach my $basename (@basenames) { my @matches = grep (/$basename/, @files); print "$matches[0]\n" if $#matches == 0; }
    No, this solution breaks if any filenames have regex-significant metachars, or are partially within a larger filename.

    In my opinion, the original solution is actually the most direct, as this is a classic set difference problem.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-25 23:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found