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";

    In reply to Getting an ANSI X.12 file's delimiters by delirium

    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.