prefect Bob has asked for the wisdom of the Perl Monks concerning the following question:

Question on file I/O

I hope someone can explain this to me.....

while($index < $#logme){ print "%%%% $index $logme[$index] %%%% "; print "################## IN THE LOOP ##########"; print OUTFILE2 $logme[$index]; print 0UTFILE2 "*********IN_THE_LOOP********"; $index++; } print OUTFILE2 " OUTFILE2_IS_GOOD ";

Facts:

  1. My data from the form is in @logme
  2. $#logme=46
  3. $index is initialized to zero.
  4. The loop is iterating (error checking using the STDOUT print).
  5. OUTFILE2 does append to my log, just not within the loop.
Problems:
My name=value pairs, which are in @logme, are not being sent to my log. I have debugged different solns for a while now and no matter what approach I try I always get to the same problem. The debug print OUTFILE2 "OUTFILE2_IS_GOOD" prints fine. Ive tried initializing a Filehandle within the loop but that didn't work either...

help!!!

Replies are listed 'Best First'.
Re: Question on file I/O
by chromatic (Archbishop) on Feb 03, 2000 at 01:55 UTC
    Try this:
    my $index = 0; foreach (@logme) { print "%%%% $index $_ %%%% "; print "################## IN THE LOOP ##########"; print OUTFILE2 $_; print 0UTFILE2 "*********IN_THE_LOOP********"; $index++; } print OUTFILE2 " OUTFILE2_IS_GOOD ";
    I'm not sure you want to print the line with the asterisks to OUTFILE2, but you know how to get rid of that if you don't. You can even shorten the third print statement by getting rid of the default scalar $_, if you're so inclined.

    Iterating through lists is much, much easier with foreach. Love it.

Got it (Re: Question on file I/O)
by prefect Bob (Initiate) on Jan 30, 2000 at 23:03 UTC
    I'm logging in and working remotely, this has given me an unexpected problem with file permissions.
Re: Question on file I/O
by Anonymous Monk on Feb 01, 2000 at 15:47 UTC
    Apart from that, you have a off-by-one error. $#logme list the last index of @logme; you start $index at 0, but the loop is while $index < $#logme, so the last item in @logme will never get into OUTFILE2 it would be correct if you asked for the scalar value of @logme, that gives you the # of items in @logme.
Re: Question on file I/O
by stephen (Priest) on Feb 01, 2000 at 04:06 UTC
    Yep, for future reference you'll want to make sure that you open your file with:
    open(OUTFILE2, '> $filename') or die "Couldn't open '$filename': $!; stopped"
    which would have told you what was going on at once.