[XOTcl] Referencing values in an array
- From: Daniel Kirsch <Iwillnotread_daniel@xxxxxx>
- Date: Mon, 28 Jul 2008 17:53:20 +0200
I want to fill an array in a method. The array will be passed to the method and referenced using upvar. Each element in the array should be an instance of the class MyShape:
package require XOTcl
Class MyShape -slots {
Attribute id;
};
MyShape instproc init {id} {
my id $id;
};
proc getShapes {_shapes} {
upvar 1 $_shapes shapes;
for {set i 0} {$i < 5} {incr i} {
# create a MyShape instance. Save it as "shape"
MyShape shape $i;
set shapes($i) shape;
# check the id of the added element
puts "Orig: [$shapes($i) id]";
}
set count [array size shapes];
for {set x 0} {$x < $count} {incr x} {
puts "After: [$shapes($x) id]";
}
};
#define the array
array set shapes {};
# fill the array
getShapes shapes;
I create 5 separate instances of MyShape and ask for their id during creation and directly after all instances are created. During creation I get the correct id ("0" up to "4"), after creation I only get the last element for each array entries (5 times "4").
Where is my mistake?
Daniel
.
- Follow-Ups:
- Re: [XOTcl] Referencing values in an array
- From: Daniel Kirsch
- Re: [XOTcl] Referencing values in an array
- Prev by Date: Re: [XOTcl] Increment an attribute
- Next by Date: Tcl_SetStringObj question
- Previous by thread: Help! problem running a script.
- Next by thread: Re: [XOTcl] Referencing values in an array
- Index(es):
Relevant Pages
|