How to split string and store in list via TCL

前端 未结 6 1569
生来不讨喜
生来不讨喜 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:15

    The simplest way is to read all the data in, split into lines, and then use regexp with each line to extract the pieces.

    set f [open "theFile.txt"]
    set lines [split [read $f] "\n"]
    close $f
    
    set keys [set values {}]
    foreach line $lines {
        if {[regexp {^([^=]*)=(.*)$} $line -> key value]} {
            lappend keys $key
            lappend values $value
        } else {
            # No '=' in the line!!!
        }
    }
    
    # keys in $keys, values in $values
    
    puts "keys = \[[join $keys ,]\]"
    puts "values = \[[join $values ,]\]"
    

    Run that (assuming that the filename is right) and you'll get output like:

    keys = [a,b,c,d]
    values = [1,2,3,4]
    

    Collecting two lists like that might not be the best thing to do with such stuff. Often, it is better to instead to store in an array:

    # Guarded by that [regexp] inside the foreach
    set myArray($key) $value
    

    Like that, you can do lookups by name rather than having to manually search. Assuming that keys are unique and order doesn't matter.

    0 讨论(0)
  • 2021-01-03 04:16

    If replacing the equals sign characters in $data with blanks always leaves a proper, even-valued list (as in the example) it can be done a lot simpler:

    set dict [string map {= { }} $data]
    set keys [dict keys $dict]
    set values [dict values $dict]
    

    Documentation: dict, set, string

    0 讨论(0)
  • 2021-01-03 04:20

    A simple way might be using a loop:

    % set lines "a=1\nb=2\nc=3\nd=4"
    a=1
    b=2
    c=3
    d=4
    % set expressionList [split $lines "\n"]
    a=1 b=2 c=3 d=4
    % set var [list]
    % set val [list]
    % foreach i $expressionList {
        set variable [lindex [split $i "="] 0]
        set value [lindex [split $i "="] 1]
        lappend val $value
        lappend var $variable
    }
    % puts $var
    a b c d
    % puts $val
    1 2 3 4
    

    If you don't mind a regex, you might try something like this:

    % set lines "a=1\nb=2\nc=3\nd=4"
    a=1
    b=2
    c=3
    d=4
    % set var [regexp -inline -lineanchor -all -- {^[^=\n\r]+} $lines]
    a b c d
    % set val [regexp -inline -lineanchor -all -- {[^=\n\r]+$} $lines]
    1 2 3 4
    
    0 讨论(0)
  • 2021-01-03 04:23

    This is pretty old, but I would actually go about it differently... Something like the following, considering that the string is [a=1\nb=1\n ... etc.] with variable name "str":

    # determine num entries in string
    set max [llength $str]
    
    #create new strings (alph & num) based on split string
    set i 0
    set str [split $str \n]
    set alph []
    set num []
    while {$i < $max} {
        set alph "$alph [lindex [split [lindex $str $i] "="] 0]
        set num "$num [lindex [split [lindex $str $i] "="] 1]
    incr i}
    

    Maybe just personal preference, but seems simplest to me; code was not tested, but it's similar to something I was just working on.

    0 讨论(0)
  • 2021-01-03 04:27

    Following is a simple tcl code

    set s "a=1\nb=2\nc=3\nd=4"
    set s [split $s "\n"]
    foreach e $s {
        set e [split $e "="]
        lappend l1 [lindex $e 0]
        lappend l2 [lindex $e 1]
    }
    

    Now you have list l1 with [a b c d] and l2 has [1 2 3 4]

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题