in reply to Printing in a WHILE loop

Code:

#!/usr/bin/perl use strict; use warnings; #my $in_fileName = "/Users/me/test.txt"; #my $out_fileName = "/Users/me/test-out.txt"; my ($infi, $oufi, @data_array, $line, $ctr); my $txt1 = " Field:\t"; #open $infi, "<" . $in_fileName or die $!; #open $oufi, ">" . $out_fileName or die $!; while(<DATA>) { $line = $_; chomp($line); @data_array = split(/\t/, $line); $ctr = (@data_array) - 1; for ($ctr; $ctr >= 0; $ctr--) { print "$txt1,$data_array[$ctr],\t$ctr + 1, \t$.\n"; } print "Record: ", $line,"\n"; } #close $oufi; #close $infi; __DATA__ 1 2 3 4 5 6 7 8 9 10 11 12 13 1 +4 15 1a 2a 3a 4a 5a 6a 7a 8a 9a 10a 11a 12 +a 13a 14a 15a
Outupt:
Field: ,15, 14 + 1, 1 Field: ,14, 13 + 1, 1 Field: ,13, 12 + 1, 1 Field: ,12, 11 + 1, 1 Field: ,11, 10 + 1, 1 Field: ,10, 9 + 1, 1 Field: ,9, 8 + 1, 1 Field: ,8, 7 + 1, 1 Field: ,7, 6 + 1, 1 Field: ,6, 5 + 1, 1 Field: ,5, 4 + 1, 1 Field: ,4, 3 + 1, 1 Field: ,3, 2 + 1, 1 Field: ,2, 1 + 1, 1 Field: ,1, 0 + 1, 1 Record: 1 2 3 4 5 6 7 8 9 10 11 12 + 13 14 15 Field: ,15a, 14 + 1, 2 Field: ,14a, 13 + 1, 2 Field: ,13a, 12 + 1, 2 Field: ,12a, 11 + 1, 2 Field: ,11a, 10 + 1, 2 Field: ,10a, 9 + 1, 2 Field: ,9a, 8 + 1, 2 Field: ,8a, 7 + 1, 2 Field: ,7a, 6 + 1, 2 Field: ,6a, 5 + 1, 2 Field: ,5a, 4 + 1, 2 Field: ,4a, 3 + 1, 2 Field: ,3a, 2 + 1, 2 Field: ,2a, 1 + 1, 2 Field: ,1a, 0 + 1, 2 Record: 1a 2a 3a 4a 5a 6a 7a 8a 9a 10a 1 +1a 12a 13a 14a 15a

Hope that is helpful...

...the majority is always wrong, and always the last to know about it...

Insanity: Doing the same thing over and over again and expecting different results...

A solution is nothing more than a clearly stated problem...otherwise, the problem is not a problem, it is a facct

Replies are listed 'Best First'.
Re^2: Printing in a WHILE loop
by gaseous1 (Novice) on May 27, 2014 at 05:39 UTC
    Thank you very much! Excellent.