1

I want to use an array of list in tcl. This is how i initialized it:

for {set i 0} {$i<5} {incr i} {
     set defer_req$i {}
}

Its working fine. But when i use these lists in procedure, it gives an error "can't read defer_req, no such variable". please help me out

2 Answers 2

4

You have not created an array. You have created a set of variables with a common prefix of 'defer_req' and a numeric suffix. As given in the variable syntax part of the Tcl manual, array addressing uses parentheses. So your assignment statement should be

set defer_req($i) {}

and in later code that uses this you might use something like:

puts $defer_req($memberName)

You don't have to use an array - you could leave your code as it stands, creating a set of similarly named variables. In that case to use the value you would need:

puts [set defer_req$memberName]

which first runs the set statement (the part within the braces) and expands $membername into a suffix creating the full variable name. Then the set command with only one argument returns the value of the named variable.

The naive version ($defer_req$memberName) would try to substitute in the value of a variable called defer_req and concatenate its value with that of a variable called memberName.

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

Comments

1
 array set ar {}
 set ar(key) {}
 for {set i 0} {$i < 100} {incr i} {
   lappend ar(key) $i
 }
 puts $ar(key)

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.