| View previous topic :: View next topic |
| Author |
Message |
err0r Voice
Joined: 21 Apr 2012 Posts: 6
|
Posted: Wed May 02, 2012 6:09 pm Post subject: see if a number was entered |
|
|
| Code: |
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 ? |
|
| Back to top |
|
 |
caesar Mint Rubber

Joined: 14 Oct 2001 Posts: 3741 Location: Mint Factory
|
Posted: Thu May 03, 2012 12:38 am Post subject: |
|
|
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: |
regexp {^[0-9]+$} $string
|
so your code will be:
| Code: |
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. |
|
| Back to top |
|
 |
err0r Voice
Joined: 21 Apr 2012 Posts: 6
|
Posted: Thu May 03, 2012 4:12 pm Post subject: see if a number was entered |
|
|
i continue to get ,,,, that was not a number ....
what can be wrong ? |
|
| Back to top |
|
 |
caesar Mint Rubber

Joined: 14 Oct 2001 Posts: 3741 Location: Mint Factory
|
Posted: Fri May 04, 2012 12:35 am Post subject: |
|
|
| Quote: |
% 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. |
|
| Back to top |
|
 |
doggo Halfop
Joined: 05 Jan 2010 Posts: 97
|
Posted: Wed May 23, 2012 12:44 pm Post subject: |
|
|
another way id use cuz i hate regexps
| Code: |
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 "
}
}
|
_________________ NON geeky!! http://gotcode4u.com/ |
|
| Back to top |
|
 |
caesar Mint Rubber

Joined: 14 Oct 2001 Posts: 3741 Location: Mint Factory
|
Posted: Wed May 23, 2012 1:27 pm Post subject: |
|
|
Or like this:
| Code: |
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: |
% 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. |
|
| Back to top |
|
 |
|