in reply to Re^2: Can anyone make this regex job neater?
in thread Can anyone make this regex job neater?

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.