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 

string to integer

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


Joined: 18 Sep 2008
Posts: 6

PostPosted: Sun Sep 21, 2008 6:33 am    Post subject: string to integer Reply with quote

can someone please post an example of how to convert a string to an integer
_________________
Driber.net webmaster
Back to top
View user's profile Send private message Visit poster's website
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Sun Sep 21, 2008 9:13 am    Post subject: Reply with quote

That would depend on the string in question.. think you could give a couple of examples?
In most cases, scan would be your friend however.

A simple example:
Code:
set string "There are 2 users"
set int [scan $string "There are %d users"

_________________
NML_375, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
Driber
Voice


Joined: 18 Sep 2008
Posts: 6

PostPosted: Tue Sep 23, 2008 4:38 am    Post subject: Reply with quote

that's not what I'm looking for

ok here is an example:

I have $str which contains " 4" (that's a whitespace and a number four)

I need that string to be converted to an integer so that I can calculate with it
for example:
Code:
incr str 1

to make it 5


the php equivalent to this would be:
Code:
<?php
$str = " 4";
$num = (int)$str;
?>

_________________
Driber.net webmaster
Back to top
View user's profile Send private message Visit poster's website
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Tue Sep 23, 2008 12:51 pm    Post subject: Reply with quote

In this case you might be able to use the expr command. Be aware though that this command may cause some serious security issues if data is not properly sanitized, as all data will be passed through the tcl interpreter.

Of course, scan would still be an option, with a proper pattern...

A third option might be to simply strip the string of spaces using string trim, as tcl will do the necessary type-casting provided the data makes sense (i.e. string looks like an integer if it only contains digits, as a float if it only contains digits and a single dot, and so on).
_________________
NML_375, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
Driber
Voice


Joined: 18 Sep 2008
Posts: 6

PostPosted: Thu Sep 25, 2008 3:45 am    Post subject: Reply with quote

I didn't know tcl automatically does type-casting on variables, so I will give string trim a try

thanks
_________________
Driber.net webmaster
Back to top
View user's profile Send private message Visit poster's website
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