in reply to regex switch trouble

The bottom line is: "Don't use switch - it has issues".

Often you can use a dispatch table instead, although in your sample a simple hash is all that is required:

use warnings; use strict; my $text= 'bla fred wilma fred wilma bla fred fredfred'; my %replace = ( fred => 'wilma', wilma => 'fred', ); $text =~ s/(fred|wilma)/$replace{$1}/ge;

Prints:

bla wilma fred wilma fred bla wilma wilmawilma

DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: regex switch trouble
by diotalevi (Canon) on Jun 14, 2007 at 03:54 UTC

    You can drop the /e too now that you're using a dispatch table. It's even faster that way.

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊