in reply to Pattern Matching, left-to-right
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Pattern Matching, left-to-right
by Aristotle (Chancellor) on Aug 21, 2004 at 14:11 UTC | |
by Prior Nacre V (Hermit) on Aug 21, 2004 at 14:54 UTC | |
by Aristotle (Chancellor) on Aug 21, 2004 at 15:09 UTC | |
by Prior Nacre V (Hermit) on Aug 21, 2004 at 15:29 UTC |