• tcl_basic


    #*******************variable**************************
    set NAME liugaopeng
    set var 0
    #set_app_var target_library [list *.db]

    #*******************Processs Control******************
    #if...elseif...else...
    if {$var} {
    echo "Good!"
    } else {
    echo "Bad!"
    }

    #switch
    switch -regexp -exact $NAME {
    "liugaopeng" {
    echo "$NAME come to my office."
    }
    "zhaojianzhong" {
    echo "Good morning!"
    }
    default {
    echo "Error! The option is not exist."
    }
    }

    #*****************Cycle***********************
    #while
    set i 0

    while {$i < 10} {
    echo "Current value of i is $i."
    incr i 1;#i++
    }

    #for
    for {set idx 0} {$idx < 10} {incr idx 1} {
    if {$idx == 8} {
    continue
    }
    echo "Current value of idx is $idx."
    }

    set NAMES [list "liugaopeng" "chen nan" "zhaojianzhong" "lizhi"]

    #foreach
    foreach NAME $NAMES {
    echo "$NAME is here!"
    }

    set log_file_cnt 0
    set txt_file_cnt 0

    set file_list [glob *.log *.txt]

    #example
    foreach f_name $file_list {
    set f_ext [file extension $f_name]
    switch $f_ext {
    ".log" {incr log_file_cnt}
    ".txt" {incr txt_file_cnt}
    }
    }

    echo "The total number of .log file is $log_file_cnt"
    echo "The total number of .txt file is $txt_file_cnt"

    #*****************File Operation*************************************
    #Write info to it
    set SRC "Good night"
    set file_wr_id [open data.txt w+]
    puts $file_wr_id $SRC
    flush $file_wr_id
    close $file_wr_id

    #Read from a file
    set DST ""
    set file_rd_id [open data.txt r]
    gets $file_rd_id DST
    echo "Read from file is {$DST}."
    close $file_rd_id

    #****************Subroutine****************************************
    #define subroutine
    set data 10

    proc print_var {} { ;#void input
    global data
    echo "data is $data";
    }

    #Invoking subroutine
    print_var

    proc max {a b} {
    if {$a > $b} {
    set y $a
    } else {
    set y $b
    }
    return $y;
    }

    proc sum {args} {
    set num_list $args
    set sum 0
    foreach num $num_list {
    set sum [expr ($sum + $num)]
    }
    return $sum
    }

    sum 1 8 9

    array set AGES [list Liugaopeng 26 Chennan 25 Zhaojianzhong 36 lizhi 31]

    proc inc_age {name_age_list} {
    #convert list to array
    array set OLD_AGES $name_age_list
    set name_list [array names OLD_AGES]
    foreach name $name_list {
    incr OLD_AGES($name) 2
    }
    #convert array to list and output
    return [array get OLD_AGES]
    }

    inc_age [list liu 25 chen 24]

    set num 0

    proc inc_one {num} {
    upvar $num local_var
    incr local_var 2
    }

  • 相关阅读:
    使用VS2013直接升级到VS2019,Cmake编译CCC4.0版本错误记录
    最近使用LuaSqlite3时,不得不说的——事务提交ATTACH语句的巨坑
    LuaSocket学习之tcp服务端
    LuaSocket HTTP 初识记录
    LuaRocks安装教程
    Lua5.1-----函数可变参数详解
    DataTable与json互转,字段类型信息丢失问题初探
    json数组与对象数组
    asp.net中封装路由信息的对象--RouteData源码
    人类的心理行为模式(几个心理学实验)
  • 原文地址:https://www.cnblogs.com/godlovepeng/p/9499471.html
Copyright © 2020-2023  润新知