SPUG: Odd characters in string

Yitzchak Scott-Thoennes sthoenna at efn.org
Thu Jan 25 15:08:15 PST 2007


(resending, this time to the list :)

Eric.D.Peterson wrote:
> How can I count the number of odd (non-alphanumeric, on control)
> characters in a string?
>
> For example:  a string "aAA$%\/1
> 50" has  3 numeric, 3 alphabetic, 1 (or 2) control/whitespace (\n or two
> \r\n) and 4 odd characters. I thought maybe a RegEx but I keep getting
> lost with the character escaping.

Use tr///.  For instance, $string =~ tr/A-Za-z0-9//c returns a count of
how many non-alphanumerics there are.  The /c is for complement, making
the SEARCHLIST specified included rather than excluded.  tr/\x00-\x1f//
would count control characters, if that is specifically what you are after.

-- 
I'm looking for a job: http://perlmonks.org/?node=ysth#looking


More information about the spug-list mailing list