Forum Discussion
Mr_Paritchaya_7
Nimbostratus
Mar 25, 2009How to convert string variable to number?
I want to capture mobile number from http payload and doing mod it by 4 for select pool.
Now I'm confusing about how to convert string variable to integer or number variable for modulation.
...
hoolio
Cirrostratus
Mar 25, 2009Hi,
TCL variables are loosely typed so I don't think you necessarily would need to convert a string to an integer:
% expr {7 % 4}
3
% expr {0 % 4}
0
% expr {"7" % 4}
3
% expr {"7" % "4"}
3
What types of values are you trying to mod? What issues/errors are you seeing?
You do need to be careful in some scenarios when performing comparisons or operations though. Joe posted an article which describes some of this:
http://devcentral.f5.com/Default.aspx?tabid=63&articleType=ArticleView&articleId=281
7 - Understand Polymorphism
TCL is polymorphic in that variables can "morph" back and forth from data type to data type depending on how it is used. This makes loosly-typed languages like TCL easier to work with since you don't need to declare variables as a certain type such as integer or string but it does come at a cost. You can minimize those costs by using the correct operators on your data types.
* Use the correct operator for the correct type.
* Use eq,ne when comparing strings.
* Use ==,!= when comparing numbers
* Use [IP::addr] when comparing IP addresses.
If you aren't careful, things may not be what they seem as illustrated below
set x 5
if { $x == 5 } { } evaluates to true
if { $x eq 5 } { } evaluates to true
if { $x == 05 } { } evaluates to true
if { $x eq 05 } { } evaluates to false
Aaron
Recent Discussions
Related Content
DevCentral Quicklinks
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
Discover DevCentral Connects