in reply to Case-sensitive substitution with case-insensitive matches

Here is one way to do it: s/(bad)/$1 & (' ' x length $1) | 'SAD'/ie; This works because the ASCII code for space (0x20) is exactly the bit that distinguishes between ASCII upper and lower case letters.

$1 & (' ' x length $1) returns a string containing nulls and spaces. Each uppercase character in $1 produces a null in the string, and each lowercase character produces a space.

Or-ing that with 'SAD' turns on the "lowercase bit" for each letter in 'SAD' that lines up with a space in our string. The characters that line up with nulls are unchanged.

So, effectively, this copies all the lowercase bits from $1 to 'SAD'.

This is hard for me to explain, please let me know if it's not clear.

Replies are listed 'Best First'.
(Ovid) Re: Re: Case-sensitive substitution with case-insensitive matches
by Ovid (Cardinal) on Dec 02, 2000 at 05:05 UTC
    chipmunk: This is very impressive. Good work! But just to throw a wrench in there, this solution is going to have problems with EBCDIC and I don't know that it would work with UniCode (really haven't looked at it) so it does limit the portability of this really sweet hack.

    That being said, I'm really impressed :)

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.