Reputation: 11
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
Upvotes: 1
Views: 6103
Reputation: 1043
array set ar {}
set ar(key) {}
for {set i 0} {$i < 100} {incr i} {
lappend ar(key) $i
}
puts $ar(key)
Upvotes: 1
Reputation: 33193
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.
Upvotes: 4