Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: help needed with File::Find and arrays.

by osunderdog (Deacon)
on Mar 11, 2005 at 16:56 UTC ( #438721=note: print w/replies, xml ) Need Help??

in reply to help needed with File::Find and arrays.

Here is my find example. I use this when I need to remember how to use find. Course I didn't know about File::Find::Rule! Good to know.

use strict; use File::Find; print "starting a find for all files:\n"; my $files = myfind('.'); printf "Found %d files.\n", scalar(@{$files}); print "starting a find for all files matching critera:\n"; $files = myfind('.', \&criteria); printf "Found %d files.\n", scalar(@{$files}); # critera function is called by find for each file. # passed the filename found, and must return boolean # indicating whether to include the file in the result # or not. sub criteria { my $filename = shift; return $filename =~ /example/i; } # find function that will return an array or array reference # containing the files that match the criteria. # accepts two parameters: # directory to search # optional criteria function. # sub myfind { my $dir = shift; my $criteriaFunc = shift || sub{1;}; my $list = []; find({wanted=>sub{findcb($list, $criteriaFunc)}}, $dir); return((wantarray)?(@$list):($list)); } # callback function called by find each time a file is found. # this callback will add the file to the listref if it matches # the critera. sub findcb { my $listref = shift; my $criteriaFunc = shift; if($criteriaFunc->($File::Find::name)) { push @$listref, $File::Find::name; } }

"Look, Shiny Things!" is not a better business strategy than compatibility and reuse.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2023-03-21 15:34 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (60 votes). Check out past polls.