Tcl/Tk: リストから、空白だけを消したい

imoto
imoto
Altair Employee
edited October 2022 in 質問と回答 (Q&A)

set original " 1 min length         1 1.0   10.00   7.750   3.250   1.000   0.000       0";

 

上記のような、文字列の変数があった場合に、「split $original」を実行すると、下記のように返ってきます。


{} 1 min length {} {} {} {} {} {} {} {} 1 1.0 {} {} 10.00 {} {} 7.750 {} {} 3.250 {} {} 1.000 {} {} 0.000 {} {} {} {} {} {} 0

 

空白のリストだけを消したい場合は、下記のようにlremoveコマンドを使うと消すことができます。

 

lremove -all [split $original] "{}"

⇒1 min length 1 1.0 10.00 7.750 3.250 1.000 0.000 0

Answers

  • Ben Buchanan
    Ben Buchanan
    Altair Employee
    edited October 2022

    I don't know what is actually being asked because I can't read the text but from the code an easier way to do this would just be:

    set original [list  1 min length         1 1.0   10.00   7.750   3.250   1.000   0.000       0]

    This will but all the items in a list with only one space between.
    If you are looking for a comma separated list you could use:

    set original [join " 1 min length         1 1.0   10.00   7.750   3.250   1.000   0.000       0" ,]

    ⇒1,min,length,1,1.0,10.00,7.750,3.250,1.000,0.000,0

    If you are just wondering why the 'original' variable wasn't updated that is because lremove doesn't change the original list so you would have to set it to a variable. Like this

    set spacesRemoved [lremove -all [split $original] "{}"]

    Help this helps.