Tested successfully with the input you supplied:

Update

The last version did what I claimed but was still bad. Fair enough, thanks for spotting the problem Aristotle. The last version is behind the Read More .... The new version hopefully does what is wanted; here's my tests before and after changes:


Update 2

But that version was the same solution that others had :-(

Here's a third version that doesn't use regexes. TMTOWTDI!


# hl7_conv use strict; use warnings; my $input = q(AB\T\F\S\CD\E\E\E\R\R\R); my $gen_out = ''; my $exp_out = q(AB&F^CD\\E\\R~R); my %convs = ( E => '\\', F => '|', R => '~', S => '^', T => '&', ); $gen_out = hl7_replace($input); print "INPUT: $input\n"; print "GEN_OUT: $gen_out\n"; print "EXP_OUT: $exp_out\n"; print $0, ': ', $gen_out eq $exp_out ? 'SUCCESS!' : ' Z z . c8o, ', +"\n"; exit 0; sub hl7_replace { my $in = shift; my @input = split //, $in; my @output = (); for (my $i = 0; $i <= $#input; ++$i) { if ($input[$i] eq "\\" && ($i + 2) <= $#input && $convs{$input[$i + 1]} && $input[$i + 2] eq "\\") { push @output, $convs{$input[$i + 1]}; $i += 2; } else { push @output, $input[$i]; } } return join '', @output; }

Second Version Follows:

[ ~/tmp ] $ perl hl7_conv INPUT: AB\T\F\S\CD\E\E\E\R\R\R GEN_OUT: AB&F^CD\E\E~R\R EXP_OUT: AB&F^CD\E\R~R hl7_conv: Z z . c8o, [ ~/tmp ] $ perl hl7_conv INPUT: AB\T\F\S\CD\E\E\E\R\R\R GEN_OUT: AB&F^CD\E\R~R EXP_OUT: AB&F^CD\E\R~R hl7_conv: SUCCESS!

Here's the new code:

# hl7_conv use strict; use warnings; my $input = q(AB\T\F\S\CD\E\E\E\R\R\R); my $gen_out = ''; my $exp_out = q(AB&F^CD\\E\\R~R); my $rx_hl7_ctrl_chars = qr/\\([EFRST])\\/; my %convs = ( E => '\\', F => '|', R => '~', S => '^', T => '&', ); $gen_out = hl7_replace($input); print "INPUT: $input\n"; print "GEN_OUT: $gen_out\n"; print "EXP_OUT: $exp_out\n"; print $0, ': ', $gen_out eq $exp_out ? 'SUCCESS!' : ' Z z . c8o, ', +"\n"; exit 0; sub hl7_replace { my $in = shift; $in =~ s/$rx_hl7_ctrl_chars/$convs{$1}/g; return $in; }

Old Version Follows:

# hl7_conv use strict; use warnings; my $input = q(AB\T\F\S\CD); my $gen_out = ''; my $exp_out = q(AB&F^CD); my %convs = ( '|' => qr/\\F\\/, '^' => qr/\\S\\/, '&' => qr/\\T\\/, '~' => qr/\\R\\/, '\\' => qr/\\E\\/, ); $gen_out = hl7_replace($input); print "INPUT: $input\n"; print "GEN_OUT: $gen_out\n"; print "EXP_OUT: $exp_out\n"; print $0, ': ', $gen_out eq $exp_out ? 'SUCCESS!' : ' Z z . c8o, ', +"\n"; exit 0; sub hl7_replace { my $in = shift; foreach my $key (keys %convs) { $in =~ s/$convs{$key}/$key/g; } return $in; }

Here's the output:

[ ~/tmp ] $ perl hl7_conv INPUT: AB\T\F\S\CD GEN_OUT: AB&F^CD EXP_OUT: AB&F^CD hl7_conv: SUCCESS! [ ~/tmp ] $

Regards,

PN5


In reply to Re: Pattern Matching, left-to-right by Prior Nacre V
in thread Pattern Matching, left-to-right by TrekNoid

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.