I'm still a lowly Novice here, so be gentle oh great ones :)

While I'm not sure I can get you exactly what you're after, I can provide you some of the general logic I use to parse HL7 files. I know there's better ways to do it, but this is the project I learned Perl with, and so it's very rudimentary.

I work primarily with HL7 lab results, but the logic is pretty general for any HL7 record.

First off, I call the procedure with a file as a parameter, and assign it to the variable $b... Then call the following subroutine logic:

sub parse_lab_test { @segs = split('\r', $b); # \r is the record terminator foreach $seg (@segs) { $segtype = substr($seg, 0, 3); if ($segtype eq "MSH") { msh_fields($seg); } elsif ($segtype eq "PID") { pid_fields($seg); } elsif ($segtype eq "PV1") { pv1_fields($seg); ... etc ... } else { &segment_error; } } }
Then, for example, if you're working on the MSH segment, you'd have the following subroutine:

sub msh_fields { ($mshseg) = @_; @vals = split('\|', $mshseg); $docsource = $vals[3]; $trandate = $vals[6]; $doctype2 = $vals[7]; $rectype = $vals[8]; $unique_hl7_id = $vals[9]; if ($rectype ne 'ORU^R01'){ # Discrete Results only next MAIN; # Label that ends the script } }
Then, you just define all the HL7 segments you're interested in. In your case, you'd have $segtype = 'FT1', then call ft1_fields.

Anyways, that's how I do it... It's not the smallest or most elegant way to do it, but it's pretty easy to maintain :)

Trek


In reply to Re: Parse an ADT file by TrekNoid
in thread Parse an ADT file by skyler

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.