in reply to How to process variable length fields in delimited file.

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