in reply to Regular expression question

An alternative to using the tr/// suggested by MarkM might be to use a hash lookup table, sth like the following. This would also allow to substitute one character for multiple characters if you need that.
my %hash = ( a => 'A', b => 'B', ... ); s/(.)/$hash{$1}/eg;
Though I would suggest benchmarking to find out what works best for you (if you have a one to one correspondence between characters then tr/// should be quicker than my suggestion here).

-- Hofmator

Replies are listed 'Best First'.
Re: Re: Regular expression question
by Wonko the sane (Curate) on Jan 16, 2003 at 16:50 UTC
    s/(.)/$hash{$1}/eg
    Will end up removing all chars in the string that are not specifically listed in the hash.

    I also believe that tr/// is always faster than s///.

    #!/usr/local/bin/perl use strict; use Benchmark; timethese( 1000000, { trchange => \&trchange, schange => \&schange, }); sub trchange { my $s = q{123 abc def a b a b a b}; $s =~ tr/a/A/; } sub schange { my $s = q{123 abc def a b a b a b}; $s =~ s/a/A/g; } # Benchmark: timing 1000000 iterations of schange, trchange... # schange: 21 wallclock secs (21.39 usr + 0.01 sys = 21.40 CPU) @ 4 +6728.97/s (n=1000000) # trchange: 7 wallclock secs ( 6.41 usr + 0.01 sys = 6.42 CPU) @ 1 +55763.24/s (n=1000000)
    Wonko