### 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 different # files in that subdirectory, and store some data from those 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} = ; close(DATA) ; } } } chdir ".." ; } ################################################################################# ### Same as above, but now we use a while() loop at the inner-most 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 different # files in that subdirectory, and store some data from those 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() { $v{$mn}{$sd}{$sf} = $_ ; } close(DATA) ; } ## after falling out of the above loop, @suffix ## variables are now ("", "", "") } } chdir ".." ; }