AshMv
AshMv

Reputation: 392

Scope issue in TCL list

I am having a list globally declared.

global triaAXCord 
set triAXCord {}

a procedure setCordinates appending values to this list.

proc setCordinates {AX} {
lappend triAXCord $AX
puts "$triAXCord"
}

that puts gives the value inserted. But from other procedures when $triAXCord is printed as blank list. Since the list is globally declared why the values are not getting updated.

Upvotes: 0

Views: 56

Answers (1)

Chris Heithoff
Chris Heithoff

Reputation: 1863

Outside of the proc definition, your triAXCord variable is already at the global scope. No need to explicity declare it as belonging to the global scope.

Within the proc, you are in the proc's scope and cannot access the global scope's variable unless you make it explicit within the proc. That's different than Python where functions can access global variables by default.

Either of these two methods works within the proc:

  1. global triAXCord. This allows you to use triAXCord as a variable name in the proc.
  2. Use the :: prefix before the variable name, like lappend ::triAXCord $AX.

You could also use upvar if you need more flexibility in variable names or scopes.

Upvotes: 1

Related Questions