Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^5: File::Find: Return array of "wanted" files

by Laurent_R (Vicar)
on Oct 04, 2013 at 21:50 UTC ( #1056941=note: print w/ replies, xml ) Need Help??


in reply to Re^4: File::Find: Return array of "wanted" files
in thread File::Find: Return array of "wanted" files

Yes, in such a case, you want to declare the array before entering the loop. That's what choroba is doing in the suggested code, declaring @found right before entering the loop:

my @found; sub findstuff { my $file = $_; return unless -f $file; my $fullpath = $File::Find::name; if (fgrep(qr/regex/, $file)) { push @found, $fullpath; } }


Comment on Re^5: File::Find: Return array of "wanted" files
Download Code
Re^6: File::Find: Return array of "wanted" files
by alpha-lemming (Novice) on Oct 07, 2013 at 16:21 UTC

    So, to push to the array with the array inside the foreach block, I would do this:

    my @array1; my @array2; foreach (@array1) { undef @array2; find(\&mysub, cwd); ..do stuff with @array2 } sub mysub { my $file = $File::Find::name; ..filter stuff here... push(@array2, $file); }

    Or is there a nore elegant way of doing this?

      Why do you undef @array2 within your foreach loop? Do you need a fresh xopoty of @array2 each time? Probably not.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (9)
As of 2014-08-23 08:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (172 votes), past polls