To parse your layout CSV file, you could use one of the CSV CPAN modules, such as Text::CSV_XS. This would place your starting column positions into an array. A simple example is:
use strict; use warnings; use Data::Dumper; use Text::CSV_XS; my $csv = Text::CSV_XS->new(); # create a new object while (<DATA>) { my $status = $csv->parse($_); # parse a CSV string into fields my @columns = $csv->fields(); # get the parsed fields print Dumper(\@columns); } __DATA__ aa,bb,cc 0,2,3

which prints:

$VAR1 = [ 'aa', 'bb', 'cc' ]; $VAR1 = [ '0', '2', '3' ];

Then, you could use the column positions to format your ASCII data file, by reading in the data line by line and adding commas as follows:

use strict; use warnings; my @cols = qw (5 11); while (<DATA>) { chomp; my $row = ''; my $start = 0; for my $cs (@cols) { my $offset = $start; my $length = $cs-$start; $row .= substr($_, $offset, $length) . ','; $start = $cs; } $row .= substr($_, $start); print "$row\n"; } __DATA__ 12345678901234567890 abcdefghijklmnopqrst

this prints:

12345,678901,234567890 abcde,fghijk,lmnopqrst

Perhaps you could adapt these simple examples for your application.


In reply to Re: Please help me with this ASCII file parsing assignment by toolic
in thread Please help me with this ASCII file parsing assignment by zli034

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.