Home > OS >  How can I show result in foreach and regexp in sequence of variables
How can I show result in foreach and regexp in sequence of variables

Time:10-19

I am having difficulty writing this small code, from which the three variables present are to be output. But when running with tcl, only the first variable is displayed in duplicate: Diego Diego Diego ; and not in sequence as I would like it to be: Diego Henrique Guilherme.

My sample code:

set name0 "\[Diego\]"
set name1 "\[Henrique\]"
set name2 "\[Guilherme\]"

set lst {}

lappend lst [list $name0 $name1 $name2]

set num {0 1 2}

foreach a $lst b $num {

    set x [lindex $a $b]

    regexp "\[\[(.*?)\](.*?)\]" $x value out

    puts $out
}

I wasn't able to identify the error. If anyone can point out to me the flaw I'll be grateful.

I want to get this output: Diego Henrique Guilherme

CodePudding user response:

As written, lst become a list of one element where the first element is a list of three items.
Instead of

set lst {}
lappend lst [list $name0 $name1 $name2]

just do

set lst [list $name0 $name1 $name2]
  • Related