Decompose ANSI X12 transmission into individual documents.
use strict;
use warnings;
use Data::Dumper;
my $ediFile = shift;
my ($contents,$delim,$term,$txnCount,@transactions);
{ $/ = undef;
open my $ifh,'<',$ediFile;
$contents = <$ifh>;
}
($delim,$term) = $contents =~ m/^ISA(.).{101}(.)/;
$delim = quotemeta($delim);
@transactions = $contents =~ m/(ST$delim.+?SE$delim\d+$delim\d+$term)/
+gs;
($txnCount) = $contents =~ m/${term}GE$delim(\d+)/;
die "Parse error - transaction counts wrong"
if ($txnCount != scalar @transactions);
foreach my $transaction (@transactions) {
# put into useful form for processing
my @segments = split /$term/,$transaction;
my ($segCount) = $transaction =~ m/${term}SE$delim(\d+)/;
die "Parse error - segment counts wrong"
if ($segCount != scalar @segments);
my ($process_this);
map { push @$process_this, [split /$delim/,$_] }
@segments;
print Dumper(\$process_this);
}
EDI File:
ISA*00* *00* *02*AAAA *01*123456789 *
+160921*075
1*U*00401*000099836*0*P*:~GS*FA*AAAA*123456789*20160921*0751*99836*X*0
+04010~ST*9
97*998360001~AK1*SM*7311~AK9*A*1*1*1~SE*4*998360001~ST*997*998360002~A
+K1*SM*7312
~AK9*A*1*1*1~SE*4*998360002~GE*2*99836~IEA*1*000099836~
Output:
$VAR1 = \[
[
'ST',
'997',
'998360001'
],
[
'AK1',
'SM',
'7311'
],
[
'AK9',
'A',
'1',
'1',
'1'
],
[
'SE',
'4',
'998360001'
]
];
$VAR1 = \[
[
'ST',
'997',
'998360002'
],
[
'AK1',
'SM',
'7312'
],
[
'AK9',
'A',
'1',
'1',
'1'
],
[
'SE',
'4',
'998360002'
]
];
But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.