+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Perl: 997 Analyzer

  1. #1
    Jordan Guest

    Perl: 997 Analyzer

    Below is a quick and dirty 997 (Acknowledgment) analyzer. The purpose of the analyzer is to detect failures and send an email alert. I'm currently writing a PHP EDI parser class which will extend into a 997 and 824 analyzer but I needed something quick to send alerts in production now. It may take a week to finish my PHP EDI Parser so here is what I came up with (in Perl):

    [highlight="perl"]
    #!/usr/bin/perl
    # Copyright: Jordan DeLozier
    # Purpose: EDI
    #
    # Description: Send an error email about
    # rejected 997s
    #
    # Author: Jordan DeLozier
    # Date: 9-10-2008
    ################################################
    # Variables
    $input = $ARGV[0]; # File
    $error = 0;
    $type = "";
    $lineSep = "\n"; # Value will change below
    $segSep = "*"; # Value will change below
    $email = "some\@email.com"; # Email or distribution list to receive
    # rejected 997 notices.
    $from = "EDI Translator"; # Email From
    ##################################################
    # Email Subject and Message set lower


    # Open the File
    open(INFO, $input) || die ("Could not open file - $input\n");
    $edi_lines = <INFO>; # Read into array
    close(INFO); # close the file

    # Read the 106th character to determine the
    # line seperator
    $lineSep = substr($edi_lines, 105, 1);
    $segSep = "\\" . substr($edi_lines, 3, 1);

    # Split the lines
    @edi = split(/$lineSep/, $edi_lines);

    # Read Each Line
    foreach $n(@edi) {
    # Split the EDI Lines
    @elements = split(/$segSep/, $n);

    # Determine each segment
    $segment = $elements[0];

    # Grab the type for email Subject
    if ($segment eq "AK2") {
    $type = $elements[1];
    }


    # Check for the Error Codes
    if ($segment eq "AK5") {
    if ($elements[1] eq "R" || $elements[1] eq "E") {
    # Found Error, we only want one email so
    # we will set a flag here instead of sending
    # the email here.
    $error = 1;


    # CLI Output (for debugging)
    print "$n\n";
    }
    }
    }


    #kick off the email
    if ($error == 1)
    {
    # email Variables
    $subject = "$type Rejected!";
    $message = "Hello,\nAn EDI message has been rejected via 997. This problem needs correcting ASAP. Below are the contents of the EDI document.\n\n $edi_lines";
    sendEmail($email, $from, $subject, $message);

    # CLI Output
    print "Email Sent!\n";
    }


    # Simple Email Function
    # ($to, $from, $subject, $message)
    sub sendEmail
    {
    my ($to, $from, $subject, $message) = @_;
    my $sendmail = '/usr/lib/sendmail';
    open(MAIL, "|$sendmail -oi -t");
    print MAIL "From: $from\n";
    print MAIL "To: $to\n";
    print MAIL "Subject: $subject\n\n";
    print MAIL "$message\n";
    close(MAIL);
    }
    [/highlight]

    Usage:
    # ./analyze997.pl /path/to/997/file

    Sample Rejection 997:
    Code:
    ISA*00*          *00*          *01*01      *01*02 *080909*1730*U*00200*000001789*0*P*:
    GS*FA*05*02*080909*1730*1789*X*002001
    ST*997*1789
    AK1*SH*17
    AK2*856*0017
    AK5*R*3
    AK9*R*1*1*0
    SE*6*1789
    GE*1*1789
    IEA*1*000001789
    Feel free to change, use, distribute or anything you like. I've added it as an attachment as well.
    Attached Files Attached Files
    Last edited by Jordan; 09-10-2008 at 05:51 PM.

  2. CODECALL Circuit advertisement

     
  3. #2
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: Perl: 997 Analyzer

    Can you either attach the files or disable the GeSHi line numbers (or toggle the line numbers) - its a pain in the arse to copy / paste the code.

  4. #3
    Jordan Guest
    Quote Originally Posted by John View Post
    Can you either attach the files or disable the GeSHi line numbers (or toggle the line numbers) - its a pain in the arse to copy / paste the code.
    I thought I did attach it but I will once I get back on my laptop.

    Posted via CodeCall Mobile

  5. #4
    Jordan Guest

    Re: Perl: 997 Analyzer

    There you go, attached.

  6. #5
    Join Date
    Jan 2008
    Posts
    1,725
    Blog Entries
    4
    Rep Power
    29

    Re: Perl: 997 Analyzer

    Jordan,

    Why would you write the emailer in perl or php for errors in another programming language? Wouldn't an EDI error be best found in EDI itself? Is EDI capable of emailing errors to someone? I'm kind of perplexed.

  7. #6
    Jordan Guest
    Quote Originally Posted by morefood2001 View Post
    Jordan,

    Why would you write the emailer in perl or php for errors in another programming language? Wouldn't an EDI error be best found in EDI itself? Is EDI capable of emailing errors to someone? I'm kind of perplexed.
    EDI is like xml, it is just information. The EDI mapper software is code but it is probably less useful than basic in DOS 3.1. It doesn't have the ability to email or do complex algos.

    Posted via CodeCall Mobile

  8. #7
    Join Date
    Jan 2008
    Posts
    1,725
    Blog Entries
    4
    Rep Power
    29

    Re: Perl: 997 Analyzer

    Thanks for the explanation. If EDI is basically xml, why don't you and your company port over to more modern languages that are more powerful?

  9. #8
    Jordan Guest

    Re: Perl: 997 Analyzer

    It is not dictated by us. It is an automotive standard(Ford, GM, Chrysler, etc) who tell us we have to use it in order to do business with them. Other industries also use it like Walmart, for instance.

  10. #9
    Join Date
    Jan 2008
    Posts
    1,725
    Blog Entries
    4
    Rep Power
    29

    Re: Perl: 997 Analyzer

    Quote Originally Posted by Jordan View Post
    It is not dictated by us. It is an automotive standard(Ford, GM, Chrysler, etc) who tell us we have to use it in order to do business with them. Other industries also use it like Walmart, for instance.
    Wow, I can't believe how technologically outdated many modern companies are!

  11. #10
    Jordan Guest

    Re: Perl: 997 Analyzer

    I believe EDI is about 40 years old but in all that time it is still a very a very rare and unknown skill. For instance, before you met me, did you ever hear of EDI?

+ Reply to Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. javaCC semantic analyzer
    By ahmed in forum Java Help
    Replies: 0
    Last Post: 02-14-2011, 09:24 AM
  2. lexical analyzer
    By iGqala in forum Pascal and Delphi
    Replies: 1
    Last Post: 07-15-2010, 12:07 PM
  3. packet analyzer
    By ged25 in forum C# Programming
    Replies: 2
    Last Post: 12-17-2008, 08:52 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts