This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

checking if a var is a number or not

Old posts that have not been replied to for several years.
Locked
g
greenbear
Owner
Posts: 733
Joined: Mon Sep 24, 2001 8:00 pm
Location: Norway

Post by greenbear »

hi, I want to check if a var. is a number or not. until now i have been using isnum, but it doesnt work with negative numbers.

ie.
.tcl isnum 5 -> Tcl: 1
.tcl isnum -5 -> Tcl: 0

P
Petersen
Owner
Posts: 685
Joined: Thu Sep 27, 2001 8:00 pm
Location: Blackpool, UK

Post by Petersen »

Didn't I already tell you last nite that isnum isn't a valid tcl command (thus it must be a proc on your bot). For tcl versions 8.1 or higher, [string is double $string] would work. And I still can't be bothered working it out for 8.0.x :razz:
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

If you only want to tell if it's an integer (positive or negative) you could use this:

Code: Select all

proc is_integer {x} {
  if [catch {incr x}] {return 0}
  return 1
}
For a number with a decimal point, you could do a combination of regexp and expr, i.e. check to see that the string is only "+-.[0-9]", then do, if [catch {expr $x + 1}] to see if it's valid (i.e. doesn't have more than one decimal, etc).

Code: Select all

proc is_real {x} {
  set validchars {^[.+-0123456789].*$}
  if ![regexp $validchars $x] {return 0}
  if [catch {expr $x + 1}] {return 0}
  return 1
}
Locked