How to split string and store in list via TCL

前端 未结 6 1600
生来不讨喜
生来不讨喜 2021-01-03 03:56

Is there a way to split strings and save in a list ? How to split string and save in two list For example, I have a string where I split several string with =:

6条回答
  •  余生分开走
    2021-01-03 04:38

    Let say your strings placed in file abc.txt in the following order

    a=1
    b=2
    c=3
    d=4
    

    You need to create 2 lists, one for numbers and one for characters:

    set number_list [list]
    set char_list   [list]
    
    set fh [open "abc.txt" "r"]
    
    while {[gets $fh line] != -1} {
        regexp -- {(\S+)=(\S+)} $line foo char number
        lappend char_list   $char
        lappend number_list $number
    }
    
    close $fh
    
    puts $char_list
    puts $number_list
    

提交回复
热议问题