0

I have two questions:

In one of my tcl functions an array is declared and initialized as follows:

 foreach port $StcPorts {
  set dutport [lindex $DutPortsTmp $i]

  set STCPort($dutport) [list 0 [lindex [lindex $cardlist $port] 0] [lindex [lindex $cardlist $port] 1]]
}

This is pretty confusing to me since I am new to tcl. Please help me to understand the above code on STCPort creation.

Question 2:

In the same file, STCPort is being used like this:

 foreach dutport $DutPortsTmp {
    set slot [lindex $STCPort($dutport) 1]
    set port [lindex $STCPort($dutport) 2]
    set hPort [stc::create port -under $hProject -location //$stcipaddress/$slot/$port -useDefaultHost False ]
    lappend STCPort($dutport) $hPort
 }

I have sourced this file and my requirement is to get the hPort value in another function residing in another file and work on it. Below is the function d_stc:

proc d_stc {args} {
   global DutPorts STCPort 

   upvar 1 $STCPort _STCPort

   #I am trying to get hPort by array index below:
   foreach DutPort $DutPorts {
    set card [lindex $_STCPort($DutPort) 1]
    set port [lindex $_STCport($DutPort) 2]
    set hport [lindex $_STCPort($DutPort) 3]

   }

And, I am getting the following error:

 can't read "STCPort": variable is array
 while executing
"upvar 1 $STCPort _STCPort"
 (procedure "d_stc" line 3)

I have used global to access the array in this function. But, where I am going wrong?

Thanks,

1 Answer 1

2
foreach port $StcPorts {
    set dutport [lindex $DutPortsTmp $i]

    set STCPort($dutport) [list 0 [lindex [lindex $cardlist $port] 0] [lindex [lindex $cardlist $port] 1]]
}

This snippet goes through the elements of the list StcPorts, which seem to be integers representing some kind of port numbers. For every port number, an array index (stored as dutport) is generated by taking the ith element from the list DutPortsTmp. The value of i is unexplained by this snippet. Also for everly port number, an array value in the array STCPort is created, with the index created above, and a value constructed like this:

  • Get the element in the list cardlist that corresponds to the integer index port (call that C)
  • Form a list from the three items 0, the first subitem in C, and the second subitem in C.

.

foreach dutport $DutPortsTmp {
    set slot [lindex $STCPort($dutport) 1]
    set port [lindex $STCPort($dutport) 2]
    set hPort [stc::create port -under $hProject -location //$stcipaddress/$slot/$port -useDefaultHost False ]
    lappend STCPort($dutport) $hPort
}

The list DutPortsTmp, which was used to create array indexes, reappears. We work with every item in the list, with the list item/array index again being named dutport. For every iteration, we look at the corresponding array item, setting slot to the second item of the array item's value (the first item in C, above) and port to the third item of the array item's value (the second item in C). The result of an invocation of stc::create port is assigned to the variable hPort, and the value of the array item is extended with a new list item, the value in hPort.

proc d_stc {args} {
   global DutPorts STCPort 

   upvar 1 $STCPort _STCPort

   #I am trying to get hPort by array index below:
   foreach DutPort $DutPorts {
    set card [lindex $_STCPort($DutPort) 1]
    set port [lindex $_STCport($DutPort) 2]
    set hport [lindex $_STCPort($DutPort) 3]

}

This should probably be:

proc d_stc {args} {
   global DutPorts STCPort 

   foreach DutPort $DutPorts {
     set hport [lindex $STCPort($DutPort) 3]
   }
}

The corrected snippet again goes through the array indexes for STCPort, but this time DutPorts is used instead of DutPortsTmp to get the array indexes. The hport variable gets the value of the fourth item in each of the array items.

But: the value in hport is never used or returned anywhere, and the value is overwritten by a new value from each new item looked at.

upvar 1 $STCPort _STCPort

does not work since there is no string value in STCPort. If the procedure is called from the global level, this would work:

upvar 1 STCPort _STCPort

(See the difference? Not the value of STCPort, but the string "STCPort" itself.) You don't need this: the invocation of global DutPorts STCPort already makes the array STCPort visible inside the procedure.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.