[Date Prev][Date Next][Thread Prev][Thread Next]
[Search]
[Date Index]
[Thread Index]
Re: [MacPerl] How to do case-independent substitution?
Louis Chretien <lchretien@jdeq.com> writes:
}Hi everyone. Probably obvious, but I couldn't figure it out my the Camel
}book and manpages, so here goes:
}
}I must process via a perl script files comming down a serial link, and for
}each line in the file, substitute certain strings with others. I hit upon
}what I thought was a clever way of doing this (saw it somewhere on the
}list):
}
} s/\*(\w+)\*/$formats{$1}/ig;
}
}which finds any strings (identifiers, actually) enclosed by * and does a
}table lookup on the "$formats" hash. I made sure to put the "i" on the s
}command to match in a case insensitive manner.
}
}But I realize that the hash itself wouldn't match in a case insensitive
}way; if i have the string "*tilde*" and "*TILDE*", i would like that both
}of them return the "~" character.
}
}I was looking for an "upcase" or "upper" function in perl, but to no avail.
Look harder. In Perl 5 (the only Perl you should be using these days), there's
uc, as in
$x = uc "test";
print "$x\n";
There's also lc, which does the opposite. (Incidentally, I found this in
my copy of the "Programming Perl Quick Reference Guide", available from
<http://www.xs4all.nl/~jvromans/perlref.html>. There's also an inexpensive
printed version from O'Reilly.)
There's also a \U operator that I believe has shown up in later Perl 5's.
It works in MacPerl 5.13r2:
$x = "t\Uest";
print "$x\n";
I'm not sure how complicated the replacement pattern can be, but you might
first
try
s/\*(\w+)\*/$formats{uc $1}/ig
or
s/\*(\w+)\*/$formats{"\U$1"}/ig
If this doesn't work, you'll have to make your replacements a bit more
complicated, but you should have no trouble doing it.
}
}Is there a way to make sure that the matching in the hash is done in a case
}insensitive way? How could I make the $1 variable all-uppercase, so that I
}would have only one entry in the hash?
}
}Any help would be greatly appreciated.
}
}Louis Chretien
}lchretien@jdeq.com
--------
Paul J. Schinder
NASA Goddard Space Flight Center
Code 693
Greenbelt, MD 20770
schinder@pjstoaster.pg.md.us