Hi PerlMonks,

I'm trying to read in a 2-column file and print the contents in an organized manner. I tried using an array (see attached code) but I'm having some trouble making it work correctly. Any help with this would be incredibly appreciated! =)


Input 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


Desired output:
TIME: 3 ALT: 3.1 TEMP: 4.3
TIME: 2.6 ALT: 8 TEMP: 1
TIME: 6.1 ALT: 7.2 TEMP: 9.3 COLOR: 43 STRENGTH: 7


#!/usr/bin/perl #perl -e 'while (<>){print if (/^START/../^END/);}' input.txt use strict; my @array; open(my $fh, "<", "../temp/out2.asc") or die "Failed to open file: $!\n"; while(<$fh>) { if (/^START/../^END/){ chomp; push @array, $_; } } close $fh; print join " ", @array; print "\n";

In reply to 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.