| Ralf Fassel 2006-11-27, 8:03 am |
| * "comp.lang.tcl" <phillip.s.powell@gmail.com>
| I took a wild, wild guess and came up with this:
|
| [TCL]
If you post code, make sure it is suitably formatted for posting on
the Web:
- lines no longer than 70 chars, otherwise they will get wrapped and
thus are no longer valid TCL code
- continuation lines properly wrapped
- proper syntax for the mentioned language in the first place
The code you posted shows up here like this:
| proc CALCULATE_XML_EXPDATE {{triviaAttrs {}} {triviaAttrCount 0}
| {hasPassedTrivia 1}} {
| if {[string length [info procs {IS_LIST}]] == 0} then source
| ./tcl_string_tools.tcl
| if {![info exists triviaAttrs] || ![IS_LIST $triviaAttrs] || [llength
| $triviaAttrs] == 0} then \
| return [clock scan {next Saturday} -base [clock scan now]]; # RETURN
| THE NEXT SATURDAY SINCE NO expDate COULD BE FOUND
|
| # SINCE $triviaAttrs HAS TO BE AN EXISTING LIST WITH llength > 0 TO
| GET HERE, JUST MAKE SURE $triviaAttrCount REFLECTS THAT
| if {[expr {$triviaAttrCount == 0}]} then $triviaAttrCount = [expr
| {[XML_GET_ELEMENT_ATTR_COUNT $triviaAttrs]}]
|
| # GET THE LAST expDate ATTRIBUTE VALUE IN $triviaAttrs
| set expDate [lindex $triviaAttrs [expr {[lsearch $triviaAttrs
| {expDate}] + 1 + ($triviaAttrCount * (([llength $triviaAttrs] /
| $triviaAttrCount) - 1))}]]
| # WILL RETURN THE FOLLOWING SATURDAY AFTER $expDate ROUNDED
| AUTOMATICALLY TO 00:00
| return [clock scan {next Saturday} -base $expDate]
| }
| [/TCL]
No way this will run in *any* TCL interpreter, neither Windows nor
Linux.
Things to keep in mind:
- if/then/else:
You usually require parens around the 'then' part.
This will not work as expected:
if {[string length [info procs {IS_LIST}]] == 0} \
then source ./tcl_string_tools.tcl
Instead of sourcing the file it will trigger an syntax error.
You almost always need parens for the 'then' clause (and the
elseif/else as well):
if {[string length [info procs {IS_LIST}]] == 0} \
then { source ./tcl_string_tools.tcl }
Only exception is when the 'then' clause is *one* *word* only.
if {$something} then continue
if {$something} then break
But even that I would prefer to write as:
if {$something} then { continue }
if {$something} then { break }
- TCL is not Perl: your code
if {[expr {$triviaAttrCount == 0}]} \
then $triviaAttrCount = \
[expr {[XML_GET_ELEMENT_ATTR_COUNT $triviaAttrs]}]
is not TCL syntax. You want:
if {$triviaAttrCount == 0} then {
set triviaAttrCount \
[XML_GET_ELEMENT_ATTR_COUNT $triviaAttrs]
}
(no 'expr' necessary inside 'if' clauses, parens around the 'then'
clause, proper variable assignment syntax).
- called functions may fail:
| set expDate [lindex $triviaAttrs [expr {[lsearch $triviaAttrs
| {expDate}] + 1 + ($triviaAttrCount * (([llength $triviaAttrs] /
| $triviaAttrCount) - 1))}]]
The lsearch above may fail to find a match. If it does not find a
match, it returns -1. Is the calculation still valid then?
Shouldn't you handle a failure?
HTH
R'
|