If your input data really does consist of fixed-width fields, you should probably be looking at unpack, or at least be a little less explicit in your regex. Most of the time, when I've seen "fixed-width field" data, it was normal to see space padding in many of the fields, but your regex doesn't allow for this.

Anyway, since you have some sort of error-trapping already in place, and you are getting errors that seem to indicate unexpected features in the input data, your next step should be to do some more elaborate checking for data that doesn't match the expected pattern.

It'll be easier if you create a list of field names, and use a hash with those field names as keys:

# assuming a row of data in $_, and row number == $. my @fld_names = qw( begin agency district ssn serv_per_m serv_per_y serv_per_t last_name first_name middle_name cover_group pay_code pay_rate earnings holder ret_pcnt surv_ben work_sch cont_amt cont_code stuf +f ); my @fld_types = qw( \d \d+ \d+ \d+ \d+ \d+ \d \D+ \D \D \d+ \d+ \d+ \d+\D \s+ \d+ \d+ \d+ \d+\D \d+ .* ); my @fld_vals = ( /(.)(....)(...)(.{9})(..)(..)(.) (.{10})(.)(.)(.{5})(..)(.{8}) (.{7})(.{8})(....)(...)(...)(.{6})(..)(.*)/x ); # now do some testing of values... for my $i ( 0 .. $#fld_names ) { if ( $fld_vals[$i] !~ /^ $fld_types[$i] $/x ) { warn sprintf( "Data row %d: bad value in field %d (%s): %s\n", $., $i, $fld_names[$i], $fld_vals[$i] ); } } my %fields; @fields{@fld_names} = @fld_vals;
(I tried to make that match your long list of variables, but you'll want to double-check. It compiles, but is untested. Updated to remove an unwanted "$" from the "fld_types" array.)

BTW, you need to learn to put <c> ... </c> around code and data when you post -- you can update the OP to add these tags, so that the code is readable.


In reply to Re: string error message by graff
in thread string error message by sgmansell

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.