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)
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.