|
Posted by PerlFAQ Server on March 15, 2008, 3:03 pm
Please log in for more thread options
This is an excerpt from the latest version perlfaq6.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .
--------------------------------------------------------------------
6.5: How do I substitute case insensitively on the LHS while preserving case on
the RHS?
Here's a lovely Perlish solution by Larry Rosler. It exploits properties
of bitwise xor on ASCII strings.
$_= "this is a TEsT case";
$old = 'test';
$new = 'success';
s
{ uc $new | (uc $1 ^ $1) .
(uc(substr $1, -1) ^ substr $1, -1) x
(length($new) - length $1)
}egi;
print;
And here it is as a subroutine, modeled after the above:
sub preserve_case($$) {
my ($old, $new) = @_;
my $mask = uc $old ^ $old;
uc $new | $mask .
substr($mask, -1) x (length($new) - length($old))
}
$a = "this is a TEsT case";
$a =~ s/(test)/preserve_case($1, "success")/egi;
print "$a\n";
This prints:
this is a SUcCESS case
As an alternative, to keep the case of the replacement word if it is
longer than the original, you can use this code, by Jeff Pinyan:
sub preserve_case {
my ($from, $to) = @_;
my ($lf, $lt) = map length, @_;
if ($lt < $lf) { $from = substr $from, 0, $lt }
else { $from .= substr $to, $lf }
return uc $to | ($from ^ uc $from);
}
This changes the sentence to "this is a SUcCess case."
Just to show that C programmers can write C in any programming language,
if you prefer a more C-like solution, the following script makes the
substitution have the same case, letter by letter, as the original. (It
also happens to run about 240% slower than the Perlish solution runs.)
If the substitution has more characters than the string being
substituted, the case of the last character is used for the rest of the
substitution.
# Original by Nathan Torkington, massaged by Jeffrey Friedl
#
sub preserve_case($$)
{
my ($old, $new) = @_;
my ($state) = 0; # 0 = no change; 1 = lc; 2 = uc
my ($i, $oldlen, $newlen, $c) = (0, length($old),
length($new));
my ($len) = $oldlen < $newlen ? $oldlen : $newlen;
for ($i = 0; $i < $len; $i++) {
if ($c = substr($old, $i, 1), $c =~ /[\W\d_]/) {
$state = 0;
} elsif (lc $c eq $c) {
substr($new, $i, 1) = lc(substr($new, $i,
1));
$state = 1;
} else {
substr($new, $i, 1) = uc(substr($new, $i,
1));
$state = 2;
}
}
# finish up with any remaining new (for when new is longer
than old)
if ($newlen > $oldlen) {
if ($state == 1) {
substr($new, $oldlen) = lc(substr($new,
$oldlen));
} elsif ($state == 2) {
substr($new, $oldlen) = uc(substr($new,
$oldlen));
}
}
return $new;
}
--------------------------------------------------------------------
The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.
If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.
|
|
Posted by Achim Peters on March 17, 2008, 6:31 am
Please log in for more thread options
PerlFAQ Server schrieb:
>
> 6.5: How do I substitute case insensitively on the LHS while preserving case
on the RHS?
>
> And here it is as a subroutine, modeled after the above:
>
> sub preserve_case($$) {
> my ($old, $new) = @_;
> my $mask = uc $old ^ $old;
>
> uc $new | $mask .
> substr($mask, -1) x (length($new) - length($old))
> }
>
> $a = "this is a TEsT case";
I thought, $a should _NEVER_ be used as variable name, even not in
examples, except when in sorting context?
Bye
Achim
|
|
Posted by brian d foy on March 18, 2008, 5:43 pm
Please log in for more thread options wrote:
> PerlFAQ Server schrieb:
> > 6.5: How do I substitute case insensitively on the LHS while preserving
> > case on the RHS?
> > $a = "this is a TEsT case";
>
> I thought, $a should _NEVER_ be used as variable name, even not in
> examples, except when in sorting context?
Yes, using $a outside of a sort subroutine is now bad form. It's not
wrong, just bad practice. It wasn't always a bad practice though, and
most of the FAQ pre-dates people thinking like that.
I'll adjust the answer.
Thanks,
--
Posted via a free Usenet account from http://www.teranews.com
|
|
Posted by Achim Peters on March 19, 2008, 6:37 am
Please log in for more thread options brian d foy schrieb:
> wrote:
>
>> PerlFAQ Server schrieb:
>
>>> 6.5: How do I substitute case insensitively on the LHS while preserving
>>> case on the RHS?
>
>>> $a = "this is a TEsT case";
>> I thought, $a should _NEVER_ be used as variable name, even not in
>> examples, except when in sorting context?
>
> Yes, using $a outside of a sort subroutine is now bad form. It's not
> wrong, just bad practice. It wasn't always a bad practice though,
Interesting. You mean, $a and $b got their special meaning later in the
process of developing the perl language?
Things like these usually fall under the category "It's always been that
way, we don't want to change it now for compatibility reasons and for
the sake of the good ol' times ..." ;-)
But to learn - if I got that right - that the name $a was chosen _after_
the first few initial versions of a programming language instead of
something like the other perl "special" variables $[^a-zA-Z] is
surprising to me.
> I'll adjust the answer.
>
> Thanks,
Thank _you_
Bye
Achim
|
|
Posted by Peter J. Holzer on March 19, 2008, 10:16 am
Please log in for more thread options > brian d foy schrieb:
>> wrote:
>>
>>> PerlFAQ Server schrieb:
>>
>>>> 6.5: How do I substitute case insensitively on the LHS while preserving
>>>> case on the RHS?
>>
>>>> $a = "this is a TEsT case";
>>> I thought, $a should _NEVER_ be used as variable name, even not in
>>> examples, except when in sorting context?
>>
>> Yes, using $a outside of a sort subroutine is now bad form. It's not
>> wrong, just bad practice. It wasn't always a bad practice though,
>
> Interesting. You mean, $a and $b got their special meaning later in the
> process of developing the perl language?
I don't know if sort was in perl 1.0. It's pretty old, though. But $a
and $b got "more special" with the introduction of lexical variables and
use strict (perl 5.0?) and "worse practice" as it became good practice
to always use strict and lexical variables.
I agree that $a should be changed to $s (or some other non-special
variable) in the FAQ.
hp
|
| Similar Threads | Posted | | FAQ: How do I substitute case insensitively on the LHS while preserving case on the RHS? | October 17, 2004, 11:10 am |
| FAQ: How do I substitute case insensitively on the LHS while preserving case on the RHS? | October 17, 2004, 5:10 pm |
| FAQ 6.5 How do I substitute case insensitively on the LHS while preserving case on the RHS? | February 25, 2005, 12:03 pm |
| FAQ 6.5 How do I substitute case insensitively on the LHS while preserving case on the RHS? | April 9, 2005, 11:03 am |
| FAQ 6.5 How do I substitute case insensitively on the LHS while preserving case on the RHS? | June 25, 2005, 5:03 pm |
| FAQ 6.5 How do I substitute case insensitively on the LHS while preserving case on the RHS? | October 20, 2005, 4:03 pm |
| FAQ 6.5 How do I substitute case insensitively on the LHS while preserving case on the RHS? | December 13, 2005, 5:03 am |
| FAQ 6.5 How do I substitute case insensitively on the LHS while preserving case on the RHS? | January 6, 2006, 11:03 am |
| FAQ 6.5 How do I substitute case insensitively on the LHS while preserving case on the RHS? | January 13, 2006, 11:03 am |
| FAQ 6.5 How do I substitute case insensitively on the LHS while preserving case on the RHS? | May 3, 2006, 3:03 am |
|