I am trying to write a short program to convert some data in a rather bizarre format to something more useful.

The file contains n fixed width data fields (where n is known from the header of the file). Each line of the file contains up to three fields, so one record can run over more than one line, but each record starts on a new line. For example, if n=4, part of the file would look like this:

           654           -0.1192052729835885D-03 -0.1074533611698108D+02
  0.2511576310952854D+05
           655           -0.1173814075917466D-03 -0.1074770905898570D+02
  0.2511659074419869D+05
           656           -0.1169565901326905D-03 -0.1074824015295493D+02
  0.2511670643648702D+05
           657           -0.1169057197333296D-03 -0.1074839256994435D+02
  0.2511658884706037D+05
           658           -0.1229838335208557D-03 -0.1074184682541694D+02
  0.2511451830545289D+05

I want to extract this data, and write it out with one record per line. All the data I have at the moment have n=4, so at the moment I am reading the entire multiline file into a single string, and breaking it up like this:

# $data contains the entire contents of the input file while ($data =~ /\s+ (\d+) \s+ (\S+) \s+ (\S+) \s+ (\S+)/gx) { print "$1 $2 $3 $4\n"; }

This works, but I don't see how to generalise it for variable values of n. Can anyone suggest how I might modify this fragment of code, or suggest an entirely different way of solving the problem?

Thanks in advance


In reply to Converting data from file with multiline records by Bilbo

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.