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

How do you recursively pass an array by reference.
Example

@filesArray=(); &updateArray($path,\@filesArray); sub updateArray{ my $path = shift; my $myArray = shift; opendir(fp, $path); @dirList = readdir(fp); foreach $i(@dirList) { if (($i ne ".") && ($i ne "..")) { push @$fileArray, $path."/".$i; $spath = $path."/".$i; if( -d $spath ) { &GetDirArray($spath,\@filesArray); } } } closedir(fp); }

This doesn't work. It does in PHP. What is the problem. Any help would be appriciated.
Thanks

Replies are listed 'Best First'.
Re: Recusive reference to an array
by arturo (Vicar) on Mar 29, 2001 at 03:28 UTC

    Being too lazy (in that good, Perl way) to figure out an answer to the question and post some sample code, I'm going to recommend you avoid the problem entirely by using File::Find, which is designed to do recursive directory searches, and which is distributed with Perl =)

    Do a search on recurse subdirectories or even just File::Find if you want some info on how to use the module.

    HTH

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Re: Recusive reference to an array
by Masem (Monsignor) on Mar 29, 2001 at 03:31 UTC
    You get $myArray from your parameters in the second shift, but you never use it, so that array never gets filled. You probably need to change the push statement to push @$myArray, $path.'/'.$i;, and the recursive call as &GetDirArray( $path, $myArray );
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
Re: Recusive reference to an array
by little (Curate) on Mar 29, 2001 at 03:29 UTC
    let it (your script) run with -w (and even better also use strict) and you get warnings about useless use of variables, cause you gave a ref to the sub but U R working on the original not with that ref, so check filesArry and myArray
    just 0.02
    Have a nice day
    All decision is left to your taste
    Update
    Does your sub give anything back? why not? Do you really want it to modify values in the rest of your script? ok, you said that it would work in PHP , but I doubt that this is a good way to do it in PHP (and for sure it's not in perl). Better give values or refs to your sub and let it do its job on these (and only on these) and then give back a result ( as a value or a ref).
Re: Recusive reference to an array
by enoch (Chaplain) on Mar 29, 2001 at 08:31 UTC
    I was not exactly sure what you were trying to do i.e. where were you giving a value to $path and whether or not the &GetDirArray was a seperate subroutine or a typo which meant to be updateArray. At any rate, the code below should help you with your array referencing.
    #!/usr/bin/perl -w use strict; my @filesArray=(); my $path = shift @ARGV; &updateArray($path,\@filesArray); foreach my $name (@filesArray) { print $name . "\n"; } sub updateArray{ my $newPath = shift; my $arrayRef = shift; opendir(FP, $path) or die "Could not open $path: $!\n"; my $spath = ""; my @dirList = readdir(FP); foreach my $i (@dirList) { if (($i ne ".") && ($i ne "..")) { push @$arrayRef, $newPath."/".$i; $spath = $newPath."/".$i; if( -d $spath ) { &updateArray($spath,$arrayRef); } } } closedir(FP); }
    Jeremy