in reply to Building up a string from a text file

I think you need to get acquainted with "use strict". It might lead you to prefer using fewer variables overall, which would be a good thing. As for the logic in the OP, I'm puzzled. You say the ordering of input lines could be arbitrary, yet you have conditions based on the input line number. Apart from that, the "unless" block at the end is doing something that has no effect on the output: you change the value of $v2 and then don't use the modified value at all.

Here's a variant similar to the first reply, but with less typing:

use strict; my @outputs; while (<DATA>) { my @fields = split; push @fields, '' if ( @fields == 3 ); for my $i ( 0..$#fields ) { $outputs[$i] .= $fields[$i] . '|'; } } chop @outputs; # remove final '|'; print "$_\n" for ( @outputs ); __DATA__ ID1 > 66 ID2 between 6 10 ID3 < 7

Replies are listed 'Best First'.
Re^2: Building up a string from a text file
by CountZero (Bishop) on May 23, 2009 at 18:29 UTC
    Even shorter:
    use strict; my @outputs; while (<DATA>) { my @fields = split; $outputs[$_] .= $fields[$_] . '|' for 0 .. 3; } chop @outputs; # remove final '|'; print "$_\n" for ( @outputs ); __DATA__ ID1 > 66 ID2 between 6 10 ID3 < 7

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James