I strongly recommend that you use strictures (use strict; use warnings;). Providing a sample that actually runs and gives the output you expect would help a lot if you want an accurate solution.
That aside, the following code makes one pass through the file to build a hash keyed by (assumed) user id and (assumed) line reference. It then runs through the hash to generate each report line - one line per user id. Replace the elipsis with the sample data.
use strict;
use warnings;
my $data = <<DATA;
...
DATA
my %userData;
open my $inData, '<', \$data;
while (<$inData>) {
chomp;
next if /EQUITY SRFDYNAM/;
my ($userNum, $ref, $tail) = split ' ', $_, 3;
next unless defined $tail;
$userData{$userNum}{$ref} = $tail;
}
close $inData;
for my $userNum (sort keys %userData) {
my $line = $userData{$userNum}{CA0};
my $USERID = substr $line, 0, 1;
my $RECTYPE = substr $line, 1, 2;
my $ASSET = substr $line, 39, 1;
$line = $userData{$userNum}{CD0};
my $IDES = substr $line, 2, 10;
$line = $userData{$userNum}{CX4};
my $EXTK = substr $line, 2, 16;
$line = $userData{$userNum}{CD1};
my $CTRY = substr $line, 2, 3;
my $SECT = substr $line, 1, 3;
my $TYP2 = substr $line, 4, 2;
my $SICD = substr $line, 6, 4;
my $IGC = substr $line, 10, 2;
print
"$userNum,$USERID,$RECTYPE,$ASSET,$IDES,$EXTK,$CTRY,$SECT,$TYP
+2,$SICD,$IGC\n";
}
Prints:
A02545142,0,12, ,GENERALI H,GRHVF ,OUT,1OU,TF,C ,00
A03987103,0,12, ,AAP IMPLAN,APIPF ,OUT,1OU,TF,C ,38
A05345110,0,12, ,AT & S AUS,AAHKF ,OUT,1OU,TF,C ,00
Perl reduces RSI - it saves typing
|