02 – TCL String functions
Some of the functions I use in most of my TCL scripting are string-related functions. Below I’ve gathered a few of them.
Concatinate arguments
Here is an easy way to concatinate the arguments sent to a script. Useful if you want the X amount of arguments to be kept as a single argument.
Code
proc getText { text startindex } { set initialCount 0 set returnData "" foreach word [split $text " "] { set selection $word if { $initialCount >= $startindex } { if { $returnData == "" } { set returnData "$selection" } else { set returnData "$returnData $selection" } } incr initialCount 1 } return "$returnData" }
Remove formatting
When you use TCL scripting on IRC you often experience that people use all kinds of text-formatting. Bold, colors and underline. Usually you would want to strip these formattings before appending information to a database for instance.
Code
proc removeFormatting { text } { set tempText [regsub -all {\003[0-9]{0,2}(,[0-9]{0,2})?|\017|\037|\002|\026} $text "" checkText] set text $checkText return $text }
Zero Prefix a String
Here is an easy way to zero-prefix a string. This might be useful if you want to display numbers so that 100 with length 5 would be 00100 and 1000 in the same context would be 01000.
Code
proc getZeroPrefix { input length } { set remaining [expr $length - [string length $input]] set zeros "" for { set x 1 } { $x <= $remaining } { incr x } { set zeros "0$zeros" } return "$zeros$input" }
|
If you found this publication useful to you, then feel free to show your appreaciation towards open-source publications by doing a small donation. It only takes a moment of your time. Any donation is appreaciated – and would also motivate me to release more as open-source. Donations so far – $0 USD – See previous donations. |
Siste kommentarer