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.

see if a number was entered

Help for those learning Tcl or writing their own scripts.
Post Reply
e
err0r
Voice
Posts: 6
Joined: Sat Apr 21, 2012 9:00 pm

see if a number was entered

Post by err0r »

Code: Select all

bind pub - -check isnumber

proc isnumber { nick host hand chan text} {

set text [split $text]
set fbid [lindex $text 1]

if ([isnum $fbid]) {

putquick "PRIVMSG #kesh :I got a number "
  } else {
putquick "PRIVMSG #kesh :That was not a number "
          }
}

proc isnum {string} {if {([string compare $string ""]) && (![regexp {[^0-9]} $string])} then {return 1};return 0}
I have tried this, but never seems to pick up a number, any help please ?
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

That regexp is not good as it will return 0 cos of ^ and without it would return 1 if string has a number from 0 to 9 in it, else will return 1 cos of ^ and without it would return 0 if there isn't a number from 0 to 9 in it. To make it work only when only a number from 0 to 9 is found this will do it:

Code: Select all

regexp {^[0-9]+$} $string
so your code will be:

Code: Select all

proc isnumber {nick uhost hand chan text} {
    set text [split $text]
    set fbid [lindex $text 1]
    if {[regexp {^[0-9]+$} $fbid]} {
        putquick "PRIVMSG #kesh :I got a number "
    } else {
        putquick "PRIVMSG #kesh :That was not a number "
    }
}
Once the game is over, the king and the pawn go back in the same box.
e
err0r
Voice
Posts: 6
Joined: Sat Apr 21, 2012 9:00 pm

see if a number was entered

Post by err0r »

i continue to get ,,,, that was not a number ....
what can be wrong ?
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

% set text "bla"
foo
% regexp {^[0-9]+$} $text
0
% set text "bla 1"
foo 1
% regexp {^[0-9]+$} $text
0
% set text "1"
1
% regexp {^[0-9]+$} $text
1
% set text "1 "
1
% regexp {^[0-9]+$} $text
0
as you can notice the regexp will return 1 only when the $text variable only contains a number without spaces.

The fbid is the second argument in your -check command. Do you give it two arguments or just one? If just one then replace 1 with 0 in the set fbid line.
Once the game is over, the king and the pawn go back in the same box.
d
doggo
Halfop
Posts: 97
Joined: Tue Jan 05, 2010 7:53 am
Contact:

Post by doggo »

another way id use cuz i hate regexps :P

Code: Select all

proc isnumber {nick uhost hand chan text} { 
    set text [split $text] 
    set fbid [lindex $text 1] 
    if {[string is integer -strict $fbid] == 0 } {
         putquick "PRIVMSG #kesh :That was not a number "
    } else { 
         putquick "PRIVMSG #kesh :I got a number "
    } 
} 
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Or like this:

Code: Select all

proc isnumber {nick uhost hand chan text} {
	if {![scan $text {%d} number]} {
        puthelp "PRIVMSG #kesh :I got a number"
    } else {
        puthelp "PRIVMSG #kesh :That was not a number"
    }
}
that will work for a decimal integer. :)

Anyway, that regexp dose a better job as it returns 1 only if the $text is a valid number without any spaces before or after a valid decimal integer number.

Code: Select all

% set text "1 "
1
% string is integer -strict $text
1
notice the space after 1.
Once the game is over, the king and the pawn go back in the same box.
Post Reply