【问题标题】:how to remove a particular commonwords from a list?如何从列表中删除特定的常用词?
【发布时间】:2020-07-01 00:31:21
【问题描述】:

我需要从文件名列表中检索值 该列表包含

{  in2dffer34_ads.txt  in2dffer21_ads.txt in2dffer45_ads.txt in2dffer67_ads.txt }

我尝试过给予

foreach name $listsofnames {
    set fbase [file rootname [file tail $name]]
    puts $out "$fbase"
}

它给了{ in2dffer34_ads in2dffer21_ads in2dffer45_ads in2dffer67_ads } 但我只需要得到名字 喜欢

{  in2dffer34   in2dffer21  in2dffer45  in2dffer67  }

【问题讨论】:

  • 也就是说,问题是关于如何从文件名中删除“_ads”?
  • 你知道_ads是要去掉的后缀吗?

标签: arrays string list split tcl


【解决方案1】:

几种方法(在tclsh REPL 会话中):

% set names {  in2dffer34_ads.txt  in2dffer21_ads.txt in2dffer45_ads.txt in2dffer67_ads.txt }
  in2dffer34_ads.txt  in2dffer21_ads.txt in2dffer45_ads.txt in2dffer67_ads.txt 
% lmap name $names { regsub {_ads\.txt$} $name "" }
in2dffer34 in2dffer21 in2dffer45 in2dffer67
% regsub -all {_ads\.txt\M} $names ""
  in2dffer34  in2dffer21 in2dffer45 in2dffer67 
% lmap name $names { lindex [split $name _] 0 }
in2dffer34 in2dffer21 in2dffer45 in2dffer67

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-26
    • 2022-07-06
    • 2021-07-20
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 2019-08-10
    相关资源
    最近更新 更多