This is similar to GrandFather's approach moving along the line field by field a time but uses the @fieldNames array and a counter to determine whether we have an actual field or the field width of the next field.

use strict; use warnings; use feature qw{ say }; open my $dataFH, q{<}, \ <<__EOF__ or die qq{open: < HEREDOC: $!\n}; 123445678 45612 11 Steve Smith 11012015 16 1001 Main Street GA 7 Atlan +ta 30553 234256653 76467 8 Joe Blow 06072014 11 83 Low Road CO 6 Denver 12345 239879583 62098 10 Andy Pandy 03112012 13 10 The Strand NJ 13 Atlantic + City 16345 __EOF__ my @fieldNames = qw{ ssn empNo ncEmpName empName hireDate ncAddr addr state ncCity city zip }; while ( <$dataFH> ) { chomp; my $fieldCt = 0; my @fields; while ( length ) { s{^\s*}{}; my $next = $1 if s{(\S+)}{}; if ( $fieldNames[ $fieldCt ] =~ m{^nc} ) { s{^\s*}{}; push @fields, substr $_, 0, $next, q{}; $fieldCt ++; } else { push @fields, $next; } $fieldCt ++; } say join q{|}, @fields; }

The output.

123445678|45612|Steve Smith|11012015|1001 Main Street|GA|Atlanta|30553 234256653|76467|Joe Blow|06072014|83 Low Road|CO|Denver|12345 239879583|62098|Andy Pandy|03112012|10 The Strand|NJ|Atlantic City|163 +45

I hope this is of interest.

Cheers,

JohnGG


In reply to Re: How to process variable length fields in delimited file. by johngg
in thread How to process variable length fields in delimited file. by dbach355

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.