I am writing a recursive file generation algorithm that will insert a certain upper bounded number of files into a directory before creating a new directory and continuing to create the files. I am running into a problem with the recursion however, when I try to run a subroutine within the algorithm. The subroutine being called
(findDir) seems to run correctly on the first iteration, but on successive iterations no matte what value being passed to it, it seems to be preserving the initial values that were passed to it on the first pass. Can anyone shed some insight as to why this behavior is occurring?
use File::Find; use Cwd; #recursive directory creation http://perlmonks.thepen.com/183899.html my $dir = "/tstDir"; my $level_one = 0; my $level_two = 0; my $source_num = 0; my $source = "TstRun"; my $MAX_FILES_IN_DIR = 8; my $file_pointer; my $curr_dir = depthFirst($dir, 1); chdir($curr_dir); for (my $i = 0; $i < 40; $i++){ if($file_pointer = $MAX_FILES_IN_DIR){ my $curr_dir = depthFirst($dir, 1); chdir($curr_dir); } open(WRITE_FILE, ">$i"); print WRITE_FILE "$i"; close(WRITE_FILE); $file_pointer++; } print "Directory: $curr_dir\n"; #my $depth = 4; #my $count = findDir("/tstDir/0/0/TstRun0", $depth, $depth); print "count: $count\n"; sub depthFirst{ my ($dir, $depth) = @_; my $file_count = 0; if($depth eq 4){ $dir = "/tstDir/$level_one/$level_two/$source$source_num"; print "depth: $depth dir: $dir\n"; if(-e "$dir"){ $file_count = findDir($dir, 4, 4); print "file count: $count_files\n"; if($file_count < $MAX_FILES_IN_DIR){ chdir($dir); $file_pointer = $file_count; return $dir; }else{ $source_num++; depthFirst($dir, 3); } }else{ system "mkdir -p $dir"; $file_pointer = 0; return $dir; } return; }elsif($depth eq 3){ $dir = "/tstDir/$level_one/$level_two/"; print "depth: $depth dir: $dir\n"; if(-e "$dir"){ $files_count = findDir($dir, 3, 3); if($file_count < $MAX_FILES_IN_DIR){ depthFirst($dir, ++$depth); }else{ $level_two++; depthFirst($dir, 2); } }else{ system "mkdir -p $dir"; depthFirst($dir, $depth); } }elsif($depth eq 2){ $dir = "/tstDir/$level_one/"; if(-e "$dir"){ print "depth: $depth dir: $dir\n"; $file_count = findDir($dir, 2, 2); if($file_count < $MAX_FILES_IN_DIR){ depthFirst($dir, ++$depth); }else{ $level_one++; depthFirst($dir, $depth); } }else{ system "mkdir -p $dir"; depthFirst($dir, $depth); } }else{ print "depth: $depth dir: $dir\n"; depthFirst("$dir", ++$depth); } } sub findDir{ my ($dir, $min_depth, $max_depth) = @_; print "findDir: $dir min: $min_depth max: $max_depth\n"; my $count_files = 0; find( { preprocess => \&preprocess, wanted => \&wanted, }, $dir); #print "count: $count_files\n"; sub preprocess { my $depth = $File::Find::dir =~ tr[/][]; #print "depth: $depth\n"; return @_ if $depth < $max_depth; print "depth: $depth max: $max_depth\n"; if ($depth == $max_depth){ print "greping\n"; return grep { -df } @_; } return; } sub wanted { my $depth = $File::Find::dir =~ tr[/][]; return if $depth < $min_depth; print "in wanted\n"; print "file: $_\n"; if(!($_ =~ m/^\./)){ $count_files++; } } return $count_files; }

In reply to Recursion and Such by Grundle

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.