I am printing in a WHILE loop, using a text database file that is tab delimited. There is a header line, followed by several thousand data lines. Each record contains 21 fields.

When I print in the WHILE loop, very strange things happen. I am printing to STDOUT and to an output file. The STDOUT is currently used for debugging, but will eventually be used as a progress indicator. The output file will be used to store summarized data from the original file.

The madness happens with the print statements. The behavior is similar regardless of printing to STDOUT or to a file. The first line which the program evaluates in the input file, everything prints as expected. On subsequent iterations, the **only** item that prints is the $line value (actually, I think it is the $_ value). Nothing else prints.

I've looked far and wide for an answer and can't find one. Why is this happening? How do I rectify it?

#!/usr/bin/perl use strict; use warnings; my $in_fileName = "/Users/me/test.txt"; my $out_fileName = "/Users/me/test-out.txt"; my $infi; my $oufi; my @data_array; my $line; my $ctr; my $txt1 = " Field:\t"; open $infi, "<" . $in_fileName or die $!; open $oufi, ">" . $out_fileName or die $!; while(<$infi>) { $line = $_; chomp($line); @data_array = split(/\t/, $line); for ($ctr = length[@data_array]; $ctr >= 0; $ctr--) { print {*STDOUT} $txt1, $data_array[$ctr], "\t", $ctr + 1, "\t" +, $., "\n"; } print {$oufi} "Record: ", $line,"\n"; } close $oufi; close $infi;

In reply to Printing in a WHILE loop by gaseous1

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.