egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

see if a number was entered

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
err0r
Voice


Joined: 21 Apr 2012
Posts: 6

PostPosted: Wed May 02, 2012 6:09 pm    Post subject: see if a number was entered Reply with quote

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
View user's profile Send private message
caesar
Mint Rubber


Joined: 14 Oct 2001
Posts: 3741
Location: Mint Factory

PostPosted: Thu May 03, 2012 12:38 am    Post subject: Reply with quote

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
View user's profile Send private message
err0r
Voice


Joined: 21 Apr 2012
Posts: 6

PostPosted: Thu May 03, 2012 4:12 pm    Post subject: see if a number was entered Reply with quote

i continue to get ,,,, that was not a number ....
what can be wrong ?
Back to top
View user's profile Send private message
caesar
Mint Rubber


Joined: 14 Oct 2001
Posts: 3741
Location: Mint Factory

PostPosted: Fri May 04, 2012 12:35 am    Post subject: Reply with quote

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
View user's profile Send private message
doggo
Halfop


Joined: 05 Jan 2010
Posts: 97

PostPosted: Wed May 23, 2012 12:44 pm    Post subject: Reply with quote

another way id use cuz i hate regexps Razz
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
View user's profile Send private message Visit poster's website
caesar
Mint Rubber


Joined: 14 Oct 2001
Posts: 3741
Location: Mint Factory

PostPosted: Wed May 23, 2012 1:27 pm    Post subject: Reply with quote

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. Smile

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber