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.

Catching Decimal numbers in an if statement

Old posts that have not been replied to for several years.
Locked
W
Weirdo
Master
Posts: 265
Joined: Sat Apr 27, 2002 8:00 pm
Location: Manchester, England

Catching Decimal numbers in an if statement

Post by Weirdo »

Hey again :)

right, i am adding some validation to my !afk scripts. Basically. when you do

!afk 1 test

It bans you for one minute, with the reason test

(There is a reason i have this on my bot, a channel full of IRC addicts and procrastinators :P )

But, when you do

!afk 1.1 test

It bans you instantly, and for permanent. What i want to know, is how can i use an if statement to catch out these errors in the bantime? Namely the decimal points. what i ultimately want to do is to include this in the existing validation i have for this script, Which checks the number range, if that is wrong, then the script will not execute. I want to do the exact same thing with the Decimal Checker.

Any help?
W
Weirdo
Master
Posts: 265
Joined: Sat Apr 27, 2002 8:00 pm
Location: Manchester, England

Post by Weirdo »

Ok, still havent found anything that i could use for this really. Is there a way i could split the ".*" from the number entered, so it would round it down?

Code: Select all


set bantime [lindex [split $text] 0 ]

This is where the code fits into the script
T
Thor

Post by Thor »

don't understand exactly

but why not using [isnumber $string], a proc on alltools.tcl
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

The "[lindex [split $text] 0]" will get the 1.1 out of the text. But to get only the whole number, wihtout the decimal, use

Code: Select all

set bantime [lindex [split [lindex [split $text] 0] .] 0]
W
Weirdo
Master
Posts: 265
Joined: Sat Apr 27, 2002 8:00 pm
Location: Manchester, England

Post by Weirdo »

oooh, nice bit of code that, kind of confusing. So its a string inside a string. interesting indeed :)

Thanks as always, it works perfectly :)
T
Thor

Post by Thor »

yep but why people will do a 1.1
why not a 1,1 or a 1;1 or ...
W
Weirdo
Master
Posts: 265
Joined: Sat Apr 27, 2002 8:00 pm
Location: Manchester, England

Post by Weirdo »

they try to break it :)

if they use other characters in between the digits, does the "." become a "*" ?
T
Thor

Post by Thor »

i don't know why people will do a ban 1.1 (or other characters).
The ban command is

!afk time reason with time in minutes, means an integer !

The best way i think it's to allow only integers and u can test this with if [isnumber $time]

other solution, perhaps ?

Code: Select all


set test "123,4" 
set what "" 
for {set loop 0} {$loop < [strlen $test] } {incr loop} { 
    if { [regexp \[^0-9\] [string index $test $loop]]} { 
         break 
    } else { 
         set what "$what[string index $test $loop]" 
    }
} 
return $what

returns 123 in my example
W
Weirdo
Master
Posts: 265
Joined: Sat Apr 27, 2002 8:00 pm
Location: Manchester, England

Post by Weirdo »

Code: Select all

set bantime2 [lindex [split [lindex [split $text] 0] .] 1]
if {$bantime2 == ""} {set bantime2 $w3rkdefbantime}
Currently the use of !w3rk 2 2 test doesnt return anything but the $w3rkdefbantime variable. For some reason it isnt picking up the "2" which is the bantime that i want to be used.

It didnt use to do this :P
User avatar
Papillon
Owner
Posts: 724
Joined: Fri Feb 15, 2002 8:00 pm
Location: *.no

Post by Papillon »

Code: Select all

set bantime2 [lindex [split [lindex [split $text] 0] .] 1]
this is wrong.... if you trigger it with !w3rk 2 2 it will first split 2 2, then it will pick out the first element in the list.... "2", then it will try to split "2" at every . (none in this example), and now we come to where the wrong part is ;) ... it will last take elemet number 2 from a list wich only consist of 1 element... that's why $bantime2 always will be empty.. if u replace the "1" in the script with a "0" I think you'll get what u want... like this:

Code: Select all

set bantime2 [lindex [split [lindex [split $text] 0] .] 0]
Elen sila lúmenn' omentielvo
W
Weirdo
Master
Posts: 265
Joined: Sat Apr 27, 2002 8:00 pm
Location: Manchester, England

Post by Weirdo »

i really dont understand these lindex things at all. Spitting it simply i can just about get, that just confuses me still
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

First thing, it's usually easier to understand if you break up the line into separate parts.

Code: Select all

# Separate string into words
set words [split $text]

# Get first word (bantime)
set bantime [lindex $words 0]

# Split it by '.' in case there is one
set parts [split $bantime "."]

# Get the first part and ignore the rest
set real_bantime [lindex $parts 0]
The comments are a bit excessive (I wouldn't put them there for myself), but it's a lot easier to read than when everything is crammed on one line.

Just a note though, regexp would be a better function to use, I think.

regexp {(\d+)\S*\s*(.*)} $text ignore bantime reason

Read out loud, it means "at least one digit", "some non-space chars or none (e.g. the .2 part)", "some space chars (or none)", and then "any chars (the reason)".

Regexp syntax is weird at first, but then it's very logical and easy to use to parse just about anything in one line.

% set text "2;2 you're banned"
2;2 you're banned
% regexp {(\d+)\S*\s*(.*)} $text ignore bantime reason
1
% puts $bantime
2
% puts $reason
you're banned
%
Locked