顯示具有 TCL 標籤的文章。 顯示所有文章
顯示具有 TCL 標籤的文章。 顯示所有文章

2017-10-31

TCL, array 陣列

Example Code
array set familyName {
    papa Agent7
    momo Agent8
    son Agent9
}



Check ARRAY exist (1: true - 0: false)
# array exists family
# 1


List ARRAY variable name
# array name family
# sonName momoName papaName


Get ARRAY variable content
# array get family
# sonName Agent9 momoName Agent8 papaName Agent7


Resurt
# puts $familyName(papa)
# AgentPAPA

# puts $familyName(momo)
# AgentMOMO

2017-07-19

2015-08-17

TCL, MAC remove ":", and call back ":".

Example MAC is 001122998877
Change example MAC to 00:11:22:99:88:77

set macEth0 "001122998877"

for { set i 0 } {$i < 6 } { incr i } {
      set temp [format "%s" [string range $macEth0 [expr $i * 2] [expr $i * 2 + 1]]]
      append macEth0 "$temp"
      if { $i == 5 } { break }
      append macEth0 ":"
}

puts "Debug: macEth0 ==> $macEth0"


Remove MAC symbol ":"

set var_mac ""
set sfis_macEth1 ""

set var_mac [string map {: ""} $macEth0]
set sfis_macEth1 [string map {: ""} $macEth1]

puts "var_mac = $var_mac"
puts "sfis_macEth1 = $sfis_macEth1"

2015-03-25

TCL, TK

  • By default, the TK 8.4 doesn't support 
    • TTK (::ttk::), and so the package "tile" is required to support TTK.
      • install "teacup install tile"
      • code "package require tile"
    • Picture format of JPEG & PNG, and so the package "img" is required to support JPEG & PNG.
      • install "teacup install Img"
      • code "package require img::jpeg"
      • code "package require img::png"

2014-12-26

TCL, INFO exists

info exists 檢查變數是否存在.
例如 incr 指令的使用, 變數需先建立才能為變數進行加法運算. 此時可使用 info exists 先確認此變數是否存在, 再續程式.

if { ![ info exists counter ] } {
         set counter 0
} else {
         incr counter
}

TCL, File - Get file path

[Condition]
  • filename: test.tcl
  • file path: C:/02-MTS/99-Trainning/12-info/

set appPath [ file normalize [ info script ] ]

if { [file type $appPath ] == "link" } {
      set appPath [ file readlink $appPath ]
}

puts $appPath

[Program Output]
  • C:/02-MTS/99-Trainning/12-info/test.tcl

2014-11-25

TCL, MAC address increment

set var_mac 001A6A00001F
set var_incr 000000000001

set var_mac2 [ format %X [ expr "0x$var_mac + 0x$var_incr" ] ]
puts $var_mac2

set mac_next "001A$var_mac2"
puts "Next MAC Address is $mac_next"

[OUTPUT]
6A000020
Next MAC Address is 001A6A000020

[NOTE]
Line 1 to Line 4 產生的值為 Line 5 的 6A000020, 而不是 001A6A00001F, 少了 001A. 很奇怪, 所以才在 Line 7 補上 "001A", 有空再回頭瞭解.
Line 2 $var_incr 也可不用 000000000001, 可以是 1.

2014-11-17

TCL, Switch - Select right one through multi-options

set num 3

switch $num {
              " 1 " { puts "Monday" }
              " 2 " { puts "Tuesday" }
              " 3 " { puts "Wednesday" }
              " 4 " { puts "Thursday" }
              " 5 " { puts "Friday" }
              " 6 " { puts "Saturday" }
              " 7 " { puts "Sunday" }
              " default " { puts "Wrong Value" }
}

[Output]
  • Wednesday

TCL, File and Directory

Establish, Delete, Copy & Rename
Detect existing, Size & Type

[ALL Syntax]
file mkdir "directory_name"

file delete "directory_name"
file delete -force "directory_name"
file delete "file_name"

file copy  source-filename  target-filename
file copy -force  source-filename  target-filename

file rename source-filename  target-filename

file exists  filename
file isdirectory  filename
file isfile  filename

file size  filename
file type filename

TCL, File's Input & Output

[Procedure]
  • Open file --> Read/Write file --> Close file
[Syntax] 
  • Open ==> open filename "access"
  • Write ==> puts filename "content"
  • Close ==> close filename
[Example code]
  • set fd [open "$PATH/$var_sn.tmp.log" a] 
  • puts $fd "[time_str]: $msg"
  • close $fd
[Access Parameter List]
  • r : Reading a existing file
  • r+ : Reading & Writing a existing file
  • w : Empty a existing file,  and Writing the content.
  • w:  Establish a new file, and Writing the content.
  • w+ : Empty a existing file, and Writing & Reading the content.
  • w+ : Establish a new file, and Writing & Reading the content.
  • a : Append the content into the end of existing file, or Establish a new one.
  • a+ : Append (Read & Write) the content into the end of existing file, or Establish a new one.
End.

2014-10-16

TCL, TroubleShooting

Trouble-141016a]

輸入的字串無法被餵入 Console.

Example

  • puts -nonewline $Cmd "q"
  => 輸入 q 欲離開播放程式, 但指令並沒有被下到播放程式

TCL, 特殊字串使用 e.g. Ctrl+C 的字串為 \x03

US-ASCII 控制字元

Ctrl + C ... (^C) ==> \x03 .. 字元名 End of Text(正文結束)
Ctrl + X ... (^X) ==> \x18 .. 字元名 Cancel(取消)
Ctrl + [ ... (^[) ==> \x1B .. 字元名 Escape(跳脫)

2014-07-24

TCL, expr 判斷表示式的真假 & 值

EXPR (expressions) 判斷表示式 數學表示式 & 關係式表示式 的真(True) 假(False) 和 值(Value)

[Example Code]
  • set value1 [ expr 0==1 ]
  • puts $value1

  • set value2 [ expr 2 >=1 ]
  • puts $value2

  • set value3 [ expr 2+3 ]
  • puts $value3
[Output]
  • 0    ---> false
  • 1    ---> true
  • 5    ---> value
[說明]
  • Value 1: expr 去判斷 0 是否等於 1, 結果 0 不等於 1 的. 判斷為 假/ false.
  • Value 2: expr 去判斷 2 是否大或於等於 1, 結果 2 是大於 1 的. 判斷為 真/true.
  • Value 3: expr 去求得 2+3 的結果, 其結果為 5.

2014-06-06

TCL, string trim 字串剔除

剔除所需字串前後的字元.

[Syntax]
  • string  trim  string1  char  --> 刪除 string1左右char字元.
  •                                                    不指定, 則刪除空白無效字元 \t \r \n \s
  • string  trimleft  string1 char  --> 刪除 string1左邊含有char字元
  • string  trimright  string  chars  --> 刪除 string1右邊含有char字元

[ExampleCode 1]
  • set text2 "HelloWorldPapa 111 abc 222 PapaWorldHello"

  • puts $text2\n
  • puts [ string trim $text2 "Hello"]
  • puts [ string trimleft $text2 "Hello"]
  • puts [ string trimright $text2 "Hello"]
[Output]
  • HelloWorldPapa 111 abc 222 PapaWorldHello
  •  
  • WorldPapa 111 abc 222 PapaWorld
  • World Papa 111 abc 222 PapaWorldHello
  • HelloWorldPapa 111 abc 222 PapaWorld

2014-06-03

TCL, Date & Clock

[Syntax]
  • clock  format  [ clock  seconds ]  -format  "%Y%m%d%H%M%S"  ]
  • %Y - Year with century (e.g. 1990)
  • %y - Year without century (00 - 99).
  • %m - month
  • %d - Day of month (01 - 31).
  • %H - Hour in 24-hour format (00 - 23).
  • %I - Hour in 12-hour format (00 - 12).
  • %M - Minute (00 - 59).
  • %S - Seconds (00 - 59).

2014-05-29

TCL, string map 字串取代

[ExampleCode]
  • set WiFiMAC "5C:F3:70:0F:27:A1"
  • set ReMAC [ string map { : "" } $WiFiMAC ]
  • puts "After re-MAC = $ReMAC"
[OUTPUT]
  • After re-MAC = 5CF3700F27A1

2014-05-23

TCL, eval

用途: 執行字串裏的 TCL 指令.

[ExampleCode]
  • set mama "puts iLovePaPa"
  • puts $mama       #  這裡會輸出成 puts iLovePaPa

  • eval $mama       #  這裡則會輸出  iLovePaPa
[Output]
  • puts  iLovePaPa
  • iLovePapa

2014-05-14

TCL, 變數

變數
01- 存進 變數值 裏的資料都用雙引號夾 "  " 起來.
02- 變數值 裏的資料如沒有包含空白字元, 則可以不用雙引號.
03- Set 後面只能有兩個參數.
04- 變數名子前多加一個 $ 符號, 就取用變數內容.
05- 區分英文大小寫.

2014-05-13

TCL, regexp 字串比對擷取

[Scenario A]
  • 以下例為只擷取 0 ~ 9 的數字.
set a "M28SPLL1024X40R2011VTES35D121BSIRLQF"
set result [regexp -all -inline {[0-9]+} $a]
puts [concat [lrange $result 1 end-2] [lindex $result end]]

[Output]
  • 1024  40  2011  121

2014-04-15

TCL, string, compare & equal 字串處理/比對

比對字串是以 Unicode 逐一比對 string1 & string2.

compare: string1 內含字元大於 string2, 則回傳 1
compare: string1 內含字元相同 string2, 則回傳 0
compare: string1 內含字元小於 string2, 則回傳 -1
equal: string1 完全等於 string2, 則回傳 1, 反之回傳 0

syntax]:
  • string  compare  string1  string2
  • string  equal  string1  string2
parameter]:
     - nocase | 比對時不分大小寫
     - length | 決定比對的長度 "length  len"