Forum Discussion
Festus_50639
Nimbostratus
Oct 29, 2008legal comparison operator syntax
Good morning all,
I seem to have found a logic error and my first step is to rule out or confirm that my iRule is at fault.
Here is the iRule as it was used when ...
hoolio
Cirrostratus
Oct 30, 2008Hi Kevin,
I think the issue is that TCL interprets an integer with a leading zero as an octal:
http://phaseit.net/claird/comp.lang.tcl/fmm.htmlzero
ZERO: a numeric representation (examples: "5", "639.42", ...) which begins with '0' (example: "04") is interpreted as octal. It surprises some, when, for example, "expr 09 + 1" is a syntax error. The standard approach to this common situation is such an expression as expr [string trimleft $month 0] + 1
This is one of the two most common threads in common.lang.tcl (commenting is the other). The natives are sometimes grouchy on the subject.
I think the simple fix is to use %k in the clock format string to leave off the leading 0, instead of %H:
http://www.tcl.tk/man/tcl8.4/TclCmd/clock.htm
%k
Hour in 24-hour format, without leading zeros (0 - 23).
You can then use your original OR logic. Here is a quick test demonstrating the issue:
when RULE_INIT {
log local0. "========================================================="
log local0. "Test OR versus AND without leading 0 in hour"
for {set redir_time 0}{$redir_time < 24}{incr redir_time}{
log local0. "\$redir_time: $redir_time"
if {($redir_time >= 21) or ($redir_time <= 2)}{
log local0. "$redir_time: OR check was true. Redirect client to unavailable page."
} else {
log local0. "$redir_time: OR check was false. Allow client to access app."
}
if {($redir_time >= 21) and ($redir_time <= 2)}{
log local0. "$redir_time: AND check was true. Redirect client to unavailable page."
} else {
log local0. "$redir_time: AND check was false. Allow client to access app."
}
}
log local0. "---------------------------------------------------------"
log local0. "Test OR versus AND with leading 0 in hour"
for {set i 0}{$i < 24}{incr i}{
set redir_time [format "%.2d" $i]
log local0. "\$redir_time: $redir_time "
if {($redir_time >= 21) or ($redir_time <= 2)}{
log local0. "$redir_time: OR check was true. Redirect client to unavailable page."
} else {
log local0. "$redir_time: OR check was false. Allow client to access app."
}
if {($redir_time >= 21) and ($redir_time <= 2)}{
log local0. "$redir_time: AND check was true. Redirect client to unavailable page."
} else {
log local0. "$redir_time: AND check was false. Allow client to access app."
}
}
}
In the log output for the second for loop, note that the OR test is incorrect:
Rule : 08: OR check was true. Redirect client to unavailable page.
Rule : 08: AND check was false. Allow client to access app.
Rule : $redir_time: 09
Rule : 09: OR check was true. Redirect client to unavailable page.
Rule : 09: AND check was false. Allow client to access app.
Aaron
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)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
