Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
2. My second thought is to use the regexp feature (?{code}) to swap between charsets. But here I am entirely at a loss on how to implement the idea.#!/usr/bin/perl -w use strict; use diagnostics; # Mappings between character sets my %M_2_UTF8 = ( 'A' => 'X', ); # default set my %Ma_2_UTF8 = ( 'A' => 'a', ); # set a my $CURRENTSET = \%M_2_UTF8; my @setqueue = (); # a stack where we push and + pop # ESC-sequences my $switch_to_set_a = "\x1Ba"; my $reset = "\x1Bs"; # return to previous charse +t my %ESC = ( $switch_to_set_a => sub { push(@setqueue, $CURRENTSET); $CURRENTSET = \%Ma_2_UTF8; return ''; }, $reset => sub { $CURRENTSET = pop @setqueue; return ''; }, ); my $str = 'X|a|X'; my $esc_str = qq(A|${switch_to_set_a}A|${reset}A); my $convstr = fix($esc_str); if ($str eq $convstr) { print "ok\n"; } else { print "didn't work...\n"; } sub fix { my $s = shift; $s =~ s{(\x1B[abcs])|(.)} {repl($&)}ge; return $s; } sub repl { my $match = shift; if (exists $ESC{$match}) { return $ESC{$match}(); } elsif (exists $CURRENTSET->{$match}) { return $CURRENTSET->{$match}; } else { return $match; } } __END__
Yours most sincerly,
/L
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Shooting at a Moving Target
by Jenda (Abbot) on Feb 09, 2007 at 16:02 UTC | |
by Anonymous Monk on Feb 09, 2007 at 22:11 UTC | |
|
Re: Shooting at a Moving Target
by almut (Canon) on Feb 09, 2007 at 16:44 UTC | |
|
Re: Shooting at a Moving Target
by varian (Chaplain) on Feb 09, 2007 at 16:36 UTC | |
|
Re: Shooting at a Moving Target
by talexb (Chancellor) on Feb 09, 2007 at 16:48 UTC | |
|
Re: Shooting at a Moving Target
by Moron (Curate) on Feb 12, 2007 at 15:22 UTC | |
|
Re: Shooting at a Moving Target
by belg4mit (Prior) on Feb 14, 2007 at 02:33 UTC | |
by Aristotle (Chancellor) on Feb 14, 2007 at 02:55 UTC | |
by belg4mit (Prior) on Feb 14, 2007 at 03:04 UTC |