I cannot use fixed length, I have to use "substr" to find the values I need ...

I don't understand. All the examples of substr that appear in the OPed code use fixed offsets and lengths (at least, I assume you're the AnonyMonk who posted the OP). Sticking with fixed-width fields, here's another approach that might serve your needs — although I'm not really sure what those needs are! See pack for info on template specifiers; the  '@' specifier in an unpack template moves to an absolute position. Note that the  'x' specifier makes relative forward moves if you can figure out the relative displacements needed; this will save some absolute back-and-forthing. Also see perlpacktut.

>perl -wMstrict -lE "my $data = 'ACCTAxACCTBxACCTCxxxFOOxxBARxxBAZxxBOFFxxxxx'; say qq{'$data'}; ;; my ($fr1, $fr2, $fr3, $at20, $at25, $at30, $at35) = unpack '@0 a5 @6 a5 @12 a5 @20 a3 @25 a3 @30 a3 @35 a4', $dat +a; ;; say qq{'$fr1' '$fr2' '$fr3'}; ;; my $account = $fr3; ;; my $value = ($account eq 'ACCTC') ? $at20 : 'unknown'; my $end_value = ($value eq 'FOO') ? $at25 : $at30; ;; say qq{account '$account' value '$value' end value '$end_value'}; " 'ACCTAxACCTBxACCTCxxxFOOxxBARxxBAZxxBOFFxxxxx' 'ACCTA' 'ACCTB' 'ACCTC' account 'ACCTC' value 'FOO' end value 'BAR'

Update: Changed example code to simplify logic.


In reply to Re^3: Parsing a long string by AnomalousMonk
in thread Parsing a long string by Anonymous Monk

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.