No such thing as a small change | |
PerlMonks |
Question concerning tr//by greenhorn (Sexton) |
on Aug 05, 2000 at 03:03 UTC ( [id://26294]=perlquestion: print w/replies, xml ) | Need Help?? |
greenhorn has asked for the wisdom of the Perl Monks concerning the following question:
I am working on a script in which one task is to "squash" strings of certain non-alphanumeric characters (however rare it might be to find two or more of them in a row). Well,<KBD> tr/// </KBD>to the rescue. I first used:
<KBD>tr/,()@+~#$%^&*-{}[]<>\/\|//s;</KBD> It worked, sort of. But it would change a string such as<KBD> Mississippi </KBD>into<KBD> Misisipi</KBD>. ??? Then it occurred to me that perhaps the presence of the hyphen within the search-list might be making Perl "think" I'd asked for a range. This seemed pretty far-fetched, but I decided I should check the result of escaping the hyphen: <KBD>tr/,()@+~#$%^&*\-{}[]<>\/\|//s;</KBD> To my surprise, it worked; punctuation marks were "squashed" as I'd wanted. Repeated alphanumeric characters were left untouched. Moving the hyphen to the very end of the search-list also stopped the unwanted removal of alphanumeric characters. Why did the hyphen as first used have that effect, when no range resembling<KBD> A-Z </KBD>or<KBD> a-z </KBD>seems to have specified? As ever, thanks in advance . . .
Back to
Seekers of Perl Wisdom
|
|