in reply to Re: Match first word in line fails
in thread Match first word in line fails

I would make it slightly more complicated, by calling replaceword("$1") in the substitution:
sub replaceword { my $w = $_[0]; my $repl = $bad_words{lc($w)}; if (lc($w) eq $w) { lc($repl); } elsif (ucfirst($w) eq $w) { ucfirst($repl); } else { uc($repl); } }

Replies are listed 'Best First'.
Re^3: Match first word in line fails
by mr_mischief (Monsignor) on Jan 31, 2005 at 06:33 UTC
    If getting the right case is important, some extra steps may be needed. I think this version of replaceword() would be sufficient, as either lowercase or ucfirst() would be the normal casing. All-uppercase as a default disallows strange mixed case renderings, so while it would preserve some effect, it's still not a complete solution.

    sub replaceword { my $w = $_[0]; my $repl = $bad_words{ lc( $w ) }; $repl = ucfirst( $repl ) if ( $w =~ /^[A-Z]/ ); return $repl; }

    Of course, this would require that the eval flag stay on the substitution in my previous post, contrary to the update which applies to the code as appears in that node.



    Christopher E. Stith
      Yeah, I thought about something that would map case letter by letter, wrapping around if the replacement word were longer, but decided that leaving it uppercase was good enough. I didn't really think if uppercase as the default, since I would expect almost all cases to be lower or ucfirst as you say, but as a fallback for weird case (or actual uppercase input).