in reply to Re: Efficiently create an IF/ELSE regex
in thread Efficiently create an IF/ELSE regex

Thank you, this is what I was looking for:

my %whitespace = ( "\n" => "n", "\t" => "t", "\r" => "r", "\f" => "f", ); my $fixid =~ s/(\s)/\\$whitespace{$1}/g;

I find it more efficient to complete a full test, than test multiple times.

Thanks,

jcrush

Replies are listed 'Best First'.
Re^3: Efficiently create an IF/ELSE regex
by Anonymous Monk on May 01, 2015 at 20:16 UTC

    Unless you want to modify default variable $_ & save the number of replacements, there is typo in your s/// application. In that case, op should be (to save the result of all of replacements in another string) ...

    ( my $fixed = $input ) =~ s/ search / replace /x;

    For fun, try the proposed s/(\s)/\\$whitespace{$1}/g solution on string x y (a plain space in between).