[英]TCL script to extract the elements from a file in a directory and make a list
I am having one problem as stated below我有一个问题,如下所述
Problem Description问题描述
I am having some "sv" extension files, I am using "glob" to extract the matching files, Now in these matching files, I need to split them and extract the elements and create different lists.我有一些“sv”扩展文件,我正在使用“glob”来提取匹配文件,现在在这些匹配文件中,我需要拆分它们并提取元素并创建不同的列表。
For example例如
set files [glob -type f modified_rtl_files/*/*{wrapper_hsm}*]
This command gives me these two files:这个命令给了我这两个文件:
modified_rtl_files/mbist_wrapper/mbist_wpper_hsm_pkram.sv modified_rtl_files/mbi_wrapper/mbist_wrapper_hsm_sysram.sv
Now I am extracting the filename with the below command现在我正在使用以下命令提取文件名
foreach element $files {
set c [split [ file tail [ file rootname $element ] ] "_" ]
echo $c
}
This is giving me这是给我
pkram
sysram
But I need to set them to two independent list但我需要将它们设置为两个独立的列表
$mem1
$mem2
$mem1 should be pkram
$mem2 should be sysram
Whenever I am trying to create something like this, both the elements are getting printed每当我试图创建这样的东西时,这两个元素都会被打印出来
foreach element $files {
set c [split [ file tail [ file rootname $element ] ] "_" ]
set d [ lindex $c 3]
set mem1 [ lindex $d 0 ]
puts $mem1
}
It is printing both它正在打印
pkram
sysram
I want independent variables.我想要自变量。
Try adding the values you want into an array instead of numbered variables:尝试将所需的值添加到数组而不是编号变量中:
set files {modified_rtl_files/mbist_wrapper/mbist_wpper_hsm_pkram.sv modified_rtl_files/mbi_wrapper/mbist_wrapper_hsm_sysram.sv}
set mem {}
foreach f $files {
lappend mem [file rootname [lindex [split $f _] end]]
}
puts [lindex $mem 0] ;# => pkram
puts [lindex $mem 1] ;# => sysram
Or use the lmap
command to make it more concise:或者使用
lmap
命令使其更简洁:
set mem [lmap f $files {file rootname [lindex [split $f _] end]}]
You can do generated names with set
:您可以使用
set
生成名称:
foreach element [lsort $files] {
set mem[incr i] [lindex [split [file tail [file rootname $element]] "_"] 3]
puts [set mem$i]; # <<< How to *READ* a variable with a generated name
}
(Always sort the output of glob
if you care about the order at all; it's not guaranteed to come in any order at all or with any consistency). (如果您完全关心订单,请始终对
glob
的 output 进行排序;不能保证完全按任何顺序或任何一致性出现)。
BUT DON'T DO THIS!但不要这样做!
Generated variable names are a pain to work with precisely because they can't be used with the $
language syntax.生成的变量名使用起来很痛苦,因为它们不能与
$
语言语法一起使用。 You're much better using (associative) arrays because those do work with that:使用(关联)arrays 会更好,因为它们确实可以使用:
foreach element [lsort $files] {
set mem([incr i]) [lindex [split [file tail [file rootname $element]] "_"] 3]
puts $mem($i)
}
The values will not be in mem1
and mem2
, but rather mem(1)
and mem(2)
.这些值将不在
mem1
和mem2
中,而是在mem(1)
和mem(2)
中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.