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

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.