Some minor tidyling (?), a little bug fixing and a demonstration of how sample code can be altered slightly to make it run stand alone:

use warnings; use strict; my @lines = <DATA>; for my $line (@lines){ chomp $line; if($line =~ m/(^OBX\|)([^\|]*\|)([^\^]*)(.*$)/){ my ($pre, $read, $post) = ($1 . $2, $3, $4); $read =~ s/([a-zA-Z])/($1 lt 'a' ? 'U' : 'L').$1/ge; $line = "$pre$read$post"; } print "$line\n"; } __DATA__ NTE||L|obr note OBX|NM|aaA..^Haem^RD2|7.5|g/dL|13.0-18.0|OR| NTE||L|obx note 1/1 for 3058 OBX|NM|dBf..^TWC^RD2|8.9|10*9/L|4.0-11.0||

Prints:

NTE||L|obr note OBX|NM|LaLaUA..^Haem^RD2|7.5|g/dL|13.0-18.0|OR| NTE||L|obx note 1/1 for 3058 OBX|NM|LdUBLf..^TWC^RD2|8.9|10*9/L|4.0-11.0||

Note the use of <DATA> and the data section to provide the test data without requiring an additional file that has to be created somewhere and will likely require editing the code to hook up anyway. Note too that the output data is provided to make the expected outcome clear.

The two prefixing lines were changed to a single line that uses a single regext to do the prefixing. Not sure it's clearer than your code though. The same could be said for the list assignment - saves lines, but probably obfusicates the code: your call.


Perl is Huffman encoded by design.

In reply to Re^3: Can anyone make this regex job neater? by GrandFather
in thread Can anyone make this regex job neater? by Melly

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.