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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |