In the following script, it appears as if the array @suffix is getting wiped out when the while(<DATA>) hits the end of file -- @suffix is only legit for the first iteration.
This behaviour is identical on ActivePerl v5.6.1 and Perl 5.6.0 (linux-i386). Any advice would be appreciated, I think it must be a logic error on my part, but I just don't see it. (there is a simple directory structure to go along with the script if you want to run it, I can email a tar file...)
Jeff Welty
### start of bug.pl @maindir = ('DIR1', 'DIR2', 'DIR3') ; @suffix = ('s1', 's2', 's3') ; ###################################################################### +########### ### this works properly ###################################################################### +########### ### for each directory in @maindir, descend into for(@maindir) { $mn = $_ ; print STDERR $mn, "\n" ; chdir $mn ; opendir(THISDIR, ".") ; @allfiles = readdir(THISDIR) ; closedir(THISDIR) ; # for each of the files in this directory, check to see if it is # a valid subdirectory by testing for the existence of a file # named 'data.txt' in that subdirectory for(@allfiles) { $sd = $_ ; if ( -r $sd . "/data.txt" ) { # Found a valid subdirectory, now we want to open 3 differ +ent # files in that subdirectory, and store some data from tho +se files # into a multidimensional hash print STDERR $sd, "\n" ; foreach(@suffix) { $sf = $_ ; print STDERR "$mn $sd $sf\n" ; # Build the filename of the data file we want $fn = "$sd/data." . $sf ; open(DATA, "<$fn") || print "Failed to open $fn\n" ; # Store the data in a multidimensional hash $v{$mn}{$sd}{$sf} = <DATA> ; close(DATA) ; } } } chdir ".." ; } ###################################################################### +########### ### Same as above, but now we use a while(<DATA>) loop at the inner-mo +st level. ### Now it doesn't work, @suffix entries are set to "" after the first + iteration. ###################################################################### +########### ### for each directory in @maindir, descend into for(@maindir) { $mn = $_ ; print STDERR $mn, "\n" ; chdir $mn ; opendir(THISDIR, ".") ; @allfiles = readdir(THISDIR) ; closedir(THISDIR) ; # for each of the files in this directory, check to see if it is # a valid subdirectory by testing for the existence of a file # named 'data.txt' in that subdirectory for(@allfiles) { $sd = $_ ; if ( -r $sd . "/data.txt" ) { # Found a valid subdirectory, now we want to open 3 differ +ent # files in that subdirectory, and store some data from tho +se files # into a multidimensional hash print STDERR $sd, "\n" ; # uncomment the following to get the behavior expected #@suffix = ('s1', 's2', 's3') ; foreach(@suffix) { $sf = $_ ; print STDERR "$mn $sd $sf\n" ; # Build the filename of the data file we want $fn = "$sd/data." . $sf ; open(DATA, "<$fn") || print "Failed to open $fn\n" ; # Store the data in a multidimensional hash (this will + overwrite the hash, but that's # not the problem with this bug. while(<DATA>) { $v{$mn}{$sd}{$sf} = $_ ; } close(DATA) ; } ## after falling out of the above loop, @suffix ## variables are now ("", "", "") } } chdir ".." ; }
In reply to Is this a logic bug or a perl bug? by weltyj
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |