Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Question concerning tr//

by greenhorn (Sexton)
on Aug 05, 2000 at 03:03 UTC ( #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:


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:


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 . . .

Replies are listed 'Best First'.
Re: Question concerning tr//
by ColtsFoot (Chaplain) on Aug 05, 2000 at 11:33 UTC
    As btrott suggests "a range is a range" so in your original
    expression the hyphen is defining a range *-{ (i.e. all the ASCII
    characters in the range 052 to 173) so this includes the characters
    A through Z and a through z. Appart from escaping the hyphen how else
    would the interpreter know that you meant a literal hyphen?
Re: Question concerning
by btrott (Parson) on Aug 05, 2000 at 03:07 UTC
    Presumably because a range is a range, whether or not it's between A and Z, or * and {. From perlop:
    A character range may be specified with a hyphen, so tr/A-J/0-9/ does the same replacement as tr/ACEGIBDFHJ/0246813579/
    There's no mention of character ranges working only on alphanumerics. Although it does suggest that you shouldn't rely on it working properly for anything other than simple alphanumerics.
Re: Question concerning tr//
by greenhorn (Sexton) on Aug 05, 2000 at 11:39 UTC
    I must indeed have specified a range without intending to do so.

    But in:


    . . . I don't see a range clearly targeting<kbd> A-Z </kbd>or<kbd> a-z</kbd>. And yet, characters in the range<kbd> a-z </kbd>were among those "squashed".

    Update -- Coltsfoot's answer has explained this. Thanks kindly.

Re: Question concerning 'tr///'
by greenhorn (Sexton) on Aug 05, 2000 at 03:07 UTC
    Apologies, folks. The "tr///" as first placed into the subject line completely dropped out. If I can find the page on which I can edit my question, I'll fix that. (Ain't found it yet, however.)

    (Update: seems I can edit the reply. Can I edit the original as well?)

      Editor requests is the place to be,
      Monk living is the life for me...

      Sorry 'bout that folks...
      Just snapped out of a "Green Acres" flashback. <dopey grin>

      Update: Where's a personal drummer when you need one?   <rimshot>

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://26294]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2022-10-06 20:07 GMT
Find Nodes?
    Voting Booth?
    My preferred way to holiday/vacation is:

    Results (28 votes). Check out past polls.