Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array references and recursive sub calls 1

Status
Not open for further replies.

arcnon

Programmer
Aug 12, 2003
242
US
I would like to be able to call the sub like this:

my @dirs = parseDir("perl");

but recursively calling the sub to populate the same array is causing me problems. Any suggestions?


#!/usr/bin/perl
use strict;

my (@dirs,@files);

parseDir("perl");

foreach my $item (@dirs){
print ">>$item\n";
}


sub parseDir{

my $thisDir = shift;

opendir(F, "$thisDir") || die "Cannot opendir $thisDir: $!";
my @storage = readdir(F);
closedir(F);

foreach my $name (@storage) {
chomp $name;
if($name =~ /^\./){
next;
}

if( -d "$thisDir/$name" ){
#print "$thisDir/$name\n";
push(@dirs, "$thisDir/$name");
parseDir( "$thisDir/$name" );
}
else{
push(@files, "$thisDir/$name");
}
}
}
 
Try using local instead of my for @dirs.


Michael Libeson
 
the code works as submitted BUT I want to be able populate the array in the sub and return the array when it is done.
I dont know how to reference the reference as the recursion happens.
 
You mean something like:

@dirs = Parsedir('path');

sub Parsedir {
my @new_dirs = @dirs;
return @new_dirs
}


Michael Libeson
 
Instead of letting @dirs and @files be global variables (which we know are a Bad Thing), I suggest letting them be array references and passing them on each call. E.g.,
Code:
#!perl
use strict;
use warnings;

my ([b]$dirs, $files[/b]) = parseDir("c:/Perl");

foreach my $item ([b]@$dirs[/b]){
      print ">>$item\n";
}

sub parseDir{

    [b]my ($thisDir, $dirs, $files) = @_;
    my $DH;
    opendir($DH, "$thisDir") || die "Cannot opendir $thisDir: $!";
    my @storage = readdir($DH);
    closedir($DH);[/b]

    foreach my $name (@storage) {
        chomp $name;
         if($name =~ /^\./){
              next;
         }

        if( -d "$thisDir/$name" ){
            #print "$thisDir/$name\n";
            push(@$dirs, "$thisDir/$name");
            [b]parseDir( "$thisDir/$name", $dirs, $files );
         }
         else{
            push(@$files, "$thisDir/$name");
         }
    }
    return ($dirs, $files)[/b]
}
Output (partial):
Code:
>>c:/Perl/bin
>>c:/Perl/eg
>>c:/Perl/eg/aspSamples
>>c:/Perl/eg/cgi
>>c:/Perl/eg/fork
>>c:/Perl/eg/IEExamples
>>c:/Perl/eg/Windows Script Components
>>c:/Perl/eg/Windows Script Host
>>c:/Perl/html
>>c:/Perl/html/ASPNPerl
>>c:/Perl/html/ASPNPerl/img
>>c:/Perl/html/bin
>>c:/Perl/html/Components
>>c:/Perl/html/Components/Windows
>>c:/Perl/html/faq
>>c:/Perl/html/faq/Windows
>>c:/Perl/html/images
>>c:/Perl/html/lib
>>c:/Perl/html/lib/Attribute


 
that is the direction I was needing thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top