Monday, March 31, 2014

some basic tcl syntax

1) if condition
if { $a == 1 } {
    puts "\$a is 1"
}

2) variable definition
set a 10
set csv_or_assertion ""
set files [exec ls]

3) get environment variable
set run_test 0
if {[info exists env(RUN_TEST)]} {
    set run_test $env(RUN_TEST)
}

4) string operation
set csv_or_xls "CSV"
if { [string compare $csv_or_xls "CSV" ] == 0 } {
    puts "it's csv file"
}

5) array operation
#array definition
array set file_lst {}
#check array size
if {[array size file_lst] < 1} {
    exit 1
}
#loop in the array
foreach file [array name file_list] {
    puts $file
    puts $file_list($file)
}
6) list operation
#loop in the list
foreach file $file_list {
    if {[string compare $file "new book"] != 0} {
lappend $new_file_list
    }
}

7) file operation
set infile [open "filename" r]
while { [gets $infile line] >= 0 } {
    regsub {^\s*} $line "" line
    regsub {\s*$} $line "" line

    if [regexp {^#} $line] {
            continue
    }
    if [regexp {^$} $line] {
        continue
    }

    set fields [split $line " "]

    set name [lindex $fields 0]
    set path [lindex $fields 1]
    set date [lindex $fields 5]

}
close $infile

8) regexp
regsub "^.*data_path_" $name "" name

No comments:

Post a Comment