G'day matthew_t_dooley,

Welcome to the Monastery.

"Is this very ugly? Is there a more elegant way to do it?"

This is somewhat "ugly" and certainly could be made "more elegant"; however, there are issues with your code that go beyond its beauty.

"I want to process each "H00".."D99" block at a time and have used $/="\nH00"

When changing a special variable (such as $/), you should use local in an anonymous block. See "Localization of special variables".

"In order to extract the "H01" records from this set, I have used ..."

Here you've hard-coded the literal 'H01' in two places. In general, this is a bad idea. Multiple, hard-coded literals increase the chances of introducing a typo, without introducing a syntax error which Perl could tell you about, potentially causing bugs which are hard to track down. Additionally, if you need to change this literal (perhaps as part of some future code enhancement), you'll need to make multiple, identical changes: more work for you and more chances of introducing errors.

Beyond the actual code issues, there's some problems with your post. My intention here is not to berate you but rather just point out the issues.

These guidelines should help you with any future posts.

Here's my take on a better way to write the code:

#!/usr/bin/env perl -l use strict; use warnings; { local $/ = "\nH00"; while (<DATA>) { my ($h01) = / ^ ( H01 .* ) $ /mx; if (substr($h01, 14, 2) eq 'SA') { print 'WANTED: ', $h01; } } } __DATA__ H00... don't care ... H01... block1 SA ... D01... don't care ... H00... don't care ... H01... block2 SX ... D01... don't care ... H00... don't care ... H01... block3 SA ... D01... don't care ...

Output:

$ pm_1128584_process_edi_file.pl WANTED: H01... block1 SA ... WANTED: H01... block3 SA ...

For difficulties with the regular expression I've used, see perlre. Specifics of the modifiers (/.../mx) can be found in the Modifiers section of that document.

-- Ken


In reply to Re: Simple but not elegant ? by kcott
in thread Simple but not elegant ? by matthew_t_dooley

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.