Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

How to find matching file names and build a @AOA using File::Find

by chanakya (Friar)
on Jul 06, 2005 at 10:31 UTC ( [id://472744]=perlquestion: print w/replies, xml ) Need Help??

chanakya has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I have a directory in which i have data files starting with 'Afj' and 'Cfj', for example Afjiweud001234 and Cfjiweud001234.
Here the number in the filename 'Cfj...' matches with the number in the file 'Afj'.

Using File::Find how do I scan and build an output similar to the following:
@AoA = ( [ "Afjiweud001234", "Cfjiweud001234" ], [ "Afjxdfc004321", "Cfjxdfc004321"], [ "Afjaux01987", "Cfjaux01987" ], );
I am unable to get the logic for the same.
Thank you for your time

Replies are listed 'Best First'.
Re: How to find matching file names and build and @AOA using File::Find
by holli (Abbot) on Jul 06, 2005 at 11:19 UTC
    You don't need File::Find. Since all your files are in the same directory, you can use a simple opendir.
    my $dir = "c:/"; my @AoA = (); #open dir and loop over files opendir DIR, $dir or die $!; while ( my $file = readdir(DIR) ) { #skip directories next if -d "$dir/$file"; #skip files that are not interesting next unless $file =~ /^Afj(.+)/; #check for existence of corresponding file (warn "corresponding file not found: Cfj$1\n"), next unless -e "$dir/Cfj$1"; #add to array push @AoA, [$file, "Cfj$1"]; } closedir DIR;


    holli, /regexed monk/
Re: How to find matching file names and build and @AOA using File::Find
by Tanktalus (Canon) on Jul 06, 2005 at 13:00 UTC

    Usually when I see "opendir" (as in holli's response), I think "glob". Combined with a bit of map-magic, I get this:

    my @AoA = map { (my $C = $_) =~ s/^Afj/Cfj/; [ $_, $C ] } glob 'Afj*';
    This is based on the idea that both the Afj and Cfj files exist at the same time. You can tweak this a bit if that's not always true, but I'm guessing it is.

      The problem with glob is that it returns the path for every file when you do something like glob ("path/to/somewhere"). You will have to strip that path if you want just the filename. That's why I used opendir here.


      holli, /regexed monk/
Re: How to find matching file names and build and @AOA using File::Find
by kprasanna_79 (Hermit) on Jul 06, 2005 at 13:01 UTC
    Hai Chanakya,
    You can find untested code below....
    Might be you can use the logic...
    use File::Find; my @AoA = (); find(\&wanted,"."); sub wanted { push @AoA, [$file, "Cfj$1"] if($File::Find::name =~ /^Afj(.+)/); }

    --Prasanna.K
Re: How to find matching file names and build and @AOA using File::Find
by Anonymous Monk on Jul 06, 2005 at 13:00 UTC
    If your dir is '/path/to/your/dir', you can try with:
    #!/usr/bin/perl -w
    use strict;
    
    chdir '/path/to/your/dir';
    my @AoA;
    push (@AoA , map { ['Afj'.$1,'Cfj'.$1]} /^Afj(.*)/) while (<*>);
    print map{$_->[0] , " , ", $_->[1], "\n"} @AoA;
    

      Please do not use the <*> syntax. Please. Use glob '*' instead. Thanks.

Log In?
Username:
Password:

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

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

    No recent polls found