Forum Discussion

Mr_Paritchaya_7's avatar
Mr_Paritchaya_7
Icon for Nimbostratus rankNimbostratus
Mar 25, 2009

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

 

Please suggest me about this issue.

 

 

 

when HTTP_REQUEST {

 

 

set subid [findstr [HTTP::payload] "subscriberNumber" 0 ""]

 

 

now subid is string variable, I think that it can't mod.

 

 

set poolid exrp($subid % 4)

 

 

}

 

3 Replies

  • Hi,

    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
  • Hi, Aaron,

     

    From example below.

     

    I try to mod string type($subid) by 4 but I'm not sure that what type will return?

     

     

    set subid [findstr [HTTP::payload] "subscriberNumber" 0 ""]

     

    set modid expr {$subid % 4}

     

     

     

    Thank you,

     

    Paritchaya
  • Hi Paritchaya,

     

     

    What is the value for subid? Or what is the full HTTP payload?

     

     

    I think TCL will try to convert it to an integer if you're using the variable in a mod operation.

     

     

    Aaron