in reply to character test functions

We have regular expressions, and they have metacharacters and character classes.

if ( $char =~ /\d/ ) { print "It's a digit.\n"; } if ( $char =~ /\w/ ) { print "It's a word-like character.\n"; } if ( $char =~ /[a-zA-Z]/ ) { print "It's an asciibetical alpha character.\n"; } if ( $char =~ /[[:alpha:]]/ ) { print "It's an alpha character.\n"; }

...just to name a few possibilities. See perlre for details.


Dave

Replies are listed 'Best First'.
Re^2: character test functions
by ambrus (Abbot) on Nov 03, 2005 at 08:10 UTC

    Agreed, just let me add that you have to use locale; if you want these character classes to be locale-aware. This is like setlocale(LC_ALL, ""); in C, but it's only effective in the lexical scope of the declaration.

    Update: the POSIX module has an isdigit function, but you have to apply it to a string, not a character code.