Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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

by Laurent_R (Prior)
on Oct 04, 2013 at 17:14 UTC ( #1056902=note: print w/ replies, xml ) Need Help??


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

The scope of a lexical is going from where the lexical is declared to the end of the enclosing block. In the case of @found above, to the end of the file.


Comment on Re^3: File::Find: Return array of "wanted" files
Re^4: File::Find: Return array of "wanted" files
by alpha-lemming (Novice) on Oct 04, 2013 at 19:10 UTC

    Ah... so if I wanted to push from the sub to the array inside the loop, I would have to declare the array outside of the loop and reinitialise it with every iteration? Or declare the sub inside of the loop?

      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; } }

        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?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (15)
As of 2015-07-06 17:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (77 votes), past polls