SPUG: Odd characters in string

veritosproject at gmail.com veritosproject at gmail.com
Thu Jan 25 15:13:41 PST 2007


I would have said:

my $bar;
{
 my @foo = /\W/g;
 $bar = @foo;
}

But your way is easier and faster.

On 1/25/07, Yitzchak Scott-Thoennes <sthoenna at efn.org> wrote:
> (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
> _____________________________________________________________
> Seattle Perl Users Group Mailing List
>      POST TO: spug-list at pm.org
> SUBSCRIPTION: http://mail.pm.org/mailman/listinfo/spug-list
>     MEETINGS: 3rd Tuesdays
>     WEB PAGE: http://seattleperl.org/
>


More information about the spug-list mailing list