Rajat Mitra
Rajat Mitra

Reputation: 169

How do I return a list from a TCL Proc?

I have the following code -

#Create a list, call it 'p'
set p {dut.m0.x,dut.m1.y,dut.m2.z,dut.m3.z,dut.a0.vout}

#Here is a procedure to return this list 
proc get_ie_conn_ports {ie} {
        global p
        set my_list {}
        foreach n [split $p ","] {
        lappend my_list $n
    }
    return [list $my_list]
}

#This procedure call the previous procedure to retrieve the list
#It then prints it
proc print_ports_and_direction {ie} {

    set ie_ports [get_ie_conn_ports ie]
    puts $ie_ports
    foreach n [split $ie_ports (|\{|\})] {
        puts [string trim $n]
    }


}

#I call the procedure, dont worry about the argument (place holder for now)
print_ports_and_direction "dut.net00.RL_Bidir_ddiscrete_1.8"

When this list prints, I get this -

dut.m0.x dut.m1.y dut.m2.z dut.m3.z dut.a0.vout

The white space is not being accounted for. Please advise as to how I can print each member on a new line. Thanks for your help!

Upvotes: 3

Views: 6022

Answers (1)

Jerry
Jerry

Reputation: 71538

The value of ie_ports is dut.m0.x dut.m1.y dut.m2.z dut.m3.z dut.a0.vout and you are trying to split on any one of the characters ( | { } ), which are not present in ie_ports, so you will be left with the whole list.

I'm not sure what you are trying to do exactly, but you can iterate on the list itself:

foreach n $ie_ports {
    puts [string trim $n]
}

Another issue is that your procedure get_ie_conn_ports is wrapping the list $my_list in another list, which is not needed. You should return the list itself:

proc get_ie_conn_ports {ie} {
    global p
    set my_list {}
    foreach n [split $p ","] {
        lappend my_list $n
    }
    return $my_list
}

You might also want to change the following line:

set ie_ports [get_ie_conn_ports ie]

to

set ie_ports [get_ie_conn_ports $ie]

Running the modifications to your code in codepad gives the following results where each member are on a line:

dut.m0.x
dut.m1.y
dut.m2.z
dut.m3.z
dut.a0.vout

Upvotes: 2

Related Questions