This is a quick method of pulling the segment terminator and element separator out of an ANSI X.12 EDI file (ISA header only). It follows these rules:

  • Linefeed can be a either a segment terminator or a blocking character, but not both.
  • The ISA segment must start at the beginning of the file.
  • Underscore is not a valid delimiter (for the convenience of \w and \W, and my laziness)
  • #!/usr/bin/perl -w use strict; sub isa_fail { die "Problem parsing ISA segment : " . shift; } sub get_delimiters { local $/ = \4; my $chunk = <>; isa_fail "ISA can't start with $chunk" unless my($ed) = $chunk =~ +/^ISA([^\w\n])$/; $/ = \101; my $num_linefeeds = scalar @{[<> =~ /\n/g]}; $/ = \($num_linefeeds + 1); my $st = substr(<>, -1); isa_fail 'Linefeeds as both delimiter and blocking' if $st eq "\n" + && $num_linefeeds; return ($ed, $st) if $st =~ /\W/; isa_fail "Segment terminator can't be $st"; } my ($elem_sep, $seg_term) = &get_delimiters; $seg_term = 'line feed' if $seg_term eq "\n"; print "Element separator = $elem_sep\n"; print "Segment terminator = $seg_term\n";

    Replies are listed 'Best First'.
    Re: Getting an ANSI X.12 file's delimiters
    by etcshadow (Priest) on Dec 08, 2003 at 22:23 UTC
      First of all, ++ to you, simply for being (probably) the only other monk here that uses perl for X12. I'm just curious, which X12 transactions do you work with? I, coming from healthcare, do a lot with 270/271, 835 and 837. Personally, I use a somewhat more complicated library (written mostly by me) for handling X12's. So much fun.

      Anyway, as to your particular implementation: I've found that some EDI sources use (almost certainly non-compliantly... but the spec has some minor vagueries) an "\r\n" pair as their segment separator, or, even wackier include a following linefeed (and/or carriage return) after their segment separator (since this is how the spec examples tend to make the files appear). So I end up with using:

      ($isa =~ /.{105}(.?\r?\n?)/)[0]
      as my segment separator.

      P.S. In other geeky stories... have you ever had to have an argument with your boss about why "grep" is inappropriate for X12 files?

      • "Grep would be faster than using perl regexps."
      • "But it's not line-based! And why should I subshell to grep, when I'm already in a perl process?"
      • "Well... you can change it to use grep later."
      • "ARGH!"

      ------------
      :Wq
      Not an editor command: Wq
        Thanks. I hadn't given \r too much thought, as it hasn't ever bit me. We're on Unix, and most of our trading partners are Unix and Linux. (.?\r?\n?) is pretty cool, though. I'll have to remember that.

        I do mainly banking files right now (820, 828, etc.). It's my wife who is in health care, though. She works at Foresight helping develop the HIPAA validator, among other things.

        I haven't ever had to argue against the grep utility. My group knew not to use it long before I got there. A few of our TPs send data with no line breaks, and our version of Unix has a grep that can't handle > 2048 characters. Saying "grep says it can't handle the file" carries some weight, apparently.