| View previous topic :: View next topic |
| Author |
Message |
Driber Voice
Joined: 18 Sep 2008 Posts: 6
|
Posted: Sun Sep 21, 2008 6:33 am Post subject: string to integer |
|
|
can someone please post an example of how to convert a string to an integer _________________ Driber.net webmaster |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Sun Sep 21, 2008 9:13 am Post subject: |
|
|
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 |
|
 |
Driber Voice
Joined: 18 Sep 2008 Posts: 6
|
Posted: Tue Sep 23, 2008 4:38 am Post subject: |
|
|
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:
to make it 5
the php equivalent to this would be:
| Code: | <?php
$str = " 4";
$num = (int)$str;
?> |
_________________ Driber.net webmaster |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Tue Sep 23, 2008 12:51 pm Post subject: |
|
|
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 |
|
 |
Driber Voice
Joined: 18 Sep 2008 Posts: 6
|
Posted: Thu Sep 25, 2008 3:45 am Post subject: |
|
|
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 |
|
 |
|