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.
|