TCL - JackieWSC/Onepiece GitHub Wiki

TCL

Format

example 1

set a "hello"
set b "world"
puts [ format "%s - %s !!" $a $b ]

output:
hello - world !!

example 2

set a 1
set b 2
puts [ format "%d - %d !!" $a $b ]

output:
1 - 2 !!

expr

example 1

set num1 10
set num2 2

set diffNum [ expr {$num1 - $num2} ]
puts [ format "%d" $diffNum ]

output:
8

example 2

set num1 "10"
set num2 "2"

set diffNum [ expr {$num1 - $num2} ]
puts [ format "%d" $diffNum ]

otuput:
8

Time

example 1

set systemTime [clock seconds]

puts "The time is: [clock format $systemTime -format %H:%M:%S]"
puts "The date is: [clock format $systemTime -format %D]"
puts [clock format $systemTime -format {Today is: %A, the %d of %B, %Y}]
puts "\n the default format for the time is: [clock format $systemTime]\n"

output:
The time is: 12:36:33
The date is: 01/02/2018
Today is: Tuesday, the 02 of January, 2018

 the default format for the time is: Tue Jan 02 12:36:33 UTC 2018

example 2

set halBirthBook "Jan 12, 1997"
set halBirthMovie "Jan 13, 1997"
set bookSeconds [clock scan $halBirthBook -format {%b %d, %Y}]
set movieSeconds [clock scan $halBirthMovie -format {%b %d, %Y}]

puts $bookSeconds
puts $movieSeconds

set diff [ expr {$movieSeconds - $bookSeconds} ]
puts $diff

output:
853027200
853113600
86400

string

set message "testing the regex\nFetching /remote/ftp/path/file.zip to /local/dir/file/zip \
         \nconnecting to ftp..."

set target "testing the regex*"
   
puts $message
set y [string match $target $message]
puts "y:$y"

set msgList [split $message "\n"]

foreach msg $msgList {
    puts "$msg"
}

set x [lsearch $msgList Fetching*]
puts "x:$x"

set y [string match $target $message]
puts "y:$y"


set row [lindex $msgList $x]
puts "row:$row"

set result [regexp {Fetching (/[^/]+)} $row match sub1]
puts "match:$match $sub1"

set word [split $row " "]
set filename [file tail [lindex $word 1]]
puts "file:$filename"

regsub "zip" $filename "csv" csv
puts "csv:$csv"

output:
testing the regex
Fetching /remote/ftp/path/file.zip to /local/dir/file/zip  
connecting to ftp...
y:1
testing the regex
Fetching /remote/ftp/path/file.zip to /local/dir/file/zip  
connecting to ftp...
x:1
y:1
row:Fetching /remote/ftp/path/file.zip to /local/dir/file/zip  
match:Fetching /remote /remote
file:file.zip
csv:file.csv

Reference