Forum Discussion
vm_64966
Nimbostratus
Apr 14, 2008testing 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 a...
Apr 14, 2008
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
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