Forum Discussion

vm_64966's avatar
vm_64966
Icon for Nimbostratus rankNimbostratus
Apr 14, 2008

testing for empty string

Hi,

 

I am very new to iRules. Two quesitons:

 

1.) Is there a function to test if a string variable is the empty string? In my particular case, I am using regexp to populate a string variable and later in the irule, I want to know if the string was populated or not.

 

2.) How do you define an integer variable? I want to capture the output of the regexp call and store it in an integer variable and later I can test if that variable equals 0 or 1.

 

 

Thanks

7 Replies

  • 1.) Is there a function to test if a string variable is the empty string? In my particular case, I am using regexp to populate a string variable and later in the irule, I want to know if the string was populated or not.

    You can use the "string length" command to test for a length of zero. If you want to test for a null variable, then the "info exists" command is what you want.

    set val [HTTP::header "foo"]
    if { [string length $val] == 0 } {
      log local0. "foo header didn't exist"
    }

    2.) How do you define an integer variable? I want to capture the output of the regexp call and store it in an integer variable and later I can test if that variable equals 0 or 1.

    TCL variables are polymorphic in that they can be any time. TCL does it's best to assign it the correct type based on the input value but types can be coerced at runtime depending on how it is used. If you are using regexp to assign a variable and the type is an integer, the variable will be of type integer. You can then use the "==" operator on the value to determine if it's equal to zero or one.

    *DISCLAIMER*

    Be warned that there are often many ways to parse strings and regular expressions should be your last resort. If you know the format of the string, then the various "string" commands can most like accomplish what you need without incurring the overhead of the regular expression process. Regexp's are sometimes a necessary evil, but you should exhaust other options before considering using them. If you can post a snippet of what you are looking to do, we can help look for alternates to regexps.

    -Joe

  • Thanks for your help.

     

     

    I think you are right that I can get away with using "string"commands instead of regexp. But regexp is more powerful and I can accomplish the same end with fewer regexp commands than "string" commands. How can I know which one will be more efficient? Clearly if I can accomplish the same thing with 1 or 2 string commands that is preferable to 1 regexp command. But what if I need 5-6 string commands?
  • That's hard to answer exactly without something to test against and there is a lot to take into account with multiple string commands such as the overhead if you are doing temp string allocations. In general though, if you can do it with a handful of string commands, that's preferable.

     

     

    Can you post an example of what you are trying to parse? We'll see if we can come up with a decent solution for you.

     

     

    -Joe
  • Nicolas_Menant_'s avatar
    Nicolas_Menant_
    Historic F5 Account
    Just some additional stuff, if you want to see how using regexp or string commands impact your performance you have a specific command you can use to evaluate this:

     

     

    the timing command: Click here

     

     

    You have a really good examples about how to use it here: Click here
    • lbertacco's avatar
      lbertacco
      Icon for Nimbostratus rankNimbostratus

      1) I'd go with just if { $var eq "" } or if { $var ne "" } for the opposite condition.

       

  • Just some additional stuff, if you want to see how using regexp or string commands impact your performance you have a specific command you can use to evaluate this:

     

     

    the timing command: Click here

     

     

    You have a really good examples about how to use it here: Click here
    • lbertacco's avatar
      lbertacco
      Icon for Nimbostratus rankNimbostratus

      1) I'd go with just if { $var eq "" } or if { $var ne "" } for the opposite condition.