You got close

#!/usr/bin/perl use strict; my @array; #open(my $fh, "<", "../temp/out2.asc") # or die "Failed to open file: $!\n"; my $fh=\*DATA; while(<$fh>) { if (/^START/../^END/){ chomp; push @array, $_; } else{ if (scalar(@array)) { print join " ", @array; print "\n"; @array=(); } } } if (scalar(@array)) { print join " ", @array; print "\n"; @array=(); } close $fh; __DATA__ START TIME: 3 ALT: 3.1 TEMP: 4.3 END START TIME: 2.6 ALT: 8 TEMP: 1 END START TIME: 6.1 ALT: 7.2 COLOR: 43 STRENGTH: 7 TEMP: 9.3 END
Result
D:\goodies\pdhuck\down1\perl\monks>perl 1186032.pl START TIME: 3 ALT: 3.1 TEMP: 4.3 END START TIME: 2.6 ALT: 8 TEMP: 1 END START TIME: 6.1 ALT: 7.2 COLOR: 43 STRENGTH: 7 TEMP: 9.3 END
Because the if .. is true as soon as it first matches and true until after the last match, you got the start and end lines in there too.

This uses a state flag to do the collection,

use strict; my @array; #open(my $fh, "<", "../temp/out2.asc") # or die "Failed to open file: $!\n"; my $fh=\*DATA; my $collect=0; while(<$fh>) { if (/^START/) {$collect=1;} elsif(/^END/){ if (scalar(@array)) { print join " ", @array; print "\n"; @array=(); } $collect=0; } elsif($collect){ chomp; push @array, $_; } } close $fh; __DATA__ START TIME: 3 ALT: 3.1 TEMP: 4.3 END START TIME: 2.6 ALT: 8 TEMP: 1 END START TIME: 6.1 ALT: 7.2 COLOR: 43 STRENGTH: 7 TEMP: 9.3 END
result
D:\goodies\pdhuck\down1\perl\monks>perl 1186032-b.pl TIME: 3 ALT: 3.1 TEMP: 4.3 TIME: 2.6 ALT: 8 TEMP: 1 TIME: 6.1 ALT: 7.2 COLOR: 43 STRENGTH: 7 TEMP: 9.3
More like what you wanted

But what if you wanted to save it all till the end of the file loop

use strict;use warnings; my $array=[]; #open(my $fh, "<", "../temp/out2.asc") # or die "Failed to open file: $!\n"; my $fh=\*DATA; my $collect=0; my $AoA=[]; while(<$fh>) { if (/^START/) {$collect=1;} elsif(/^END/){ if (scalar(@$array)) {push @$AoA,$array;} $array=[]; $collect=0; } elsif($collect){ chomp; push @$array, $_; } } for my $a (@$AoA) { print join(' ',@$a)."\n"; } close $fh; __DATA__ START TIME: 3 ALT: 3.1 TEMP: 4.3 END START TIME: 2.6 ALT: 8 TEMP: 1 END START TIME: 6.1 ALT: 7.2 COLOR: 43 STRENGTH: 7 TEMP: 9.3 END
Result
D:\goodies\pdhuck\down1\perl\monks>perl 1186032-c.pl TIME: 3 ALT: 3.1 TEMP: 4.3 TIME: 2.6 ALT: 8 TEMP: 1 TIME: 6.1 ALT: 7.2 COLOR: 43 STRENGTH: 7 TEMP: 9.3
same result as -b but it was saved up into an array o arrays until the end of the input file and printed then.


In reply to Re: Arrays & Output printing? by huck
in thread Arrays & Output printing? by coding1227

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.