| View previous topic :: View next topic |
| Author |
Message |
willyw Revered One
Joined: 15 Jan 2009 Posts: 1175
|
Posted: Sat Apr 04, 2009 3:04 pm Post subject: How to copy one array to another |
|
|
Hello,
Very much a beginner with TCL here.
Sorry if this is a dumb question. I've fumbled around some, and not found it. If you want to direct me with a link, to some reading, that would be great.
I want to set a second array equal to the first array.
The first array has, in my rough draft, 10 elements. They can change, due to activity in the channel, and various binds.
I've got that ok, I think.
But I think the thing to do, is take a snapshot of it, when a user through another bind calls a certain procedure. This way, the info at that moment is preserved, and I can manipulate it as need be, while the first array is still doing its thing... being changed by activity in the channel.
I thought I'd be able to set array2 array1
or something like that, and array2 would be created with all its elements, just the same as array1 was, at that point in time.
I haven't found the way to do it yet, or I've typo'd it every time.
Any help or direction will be appreciated.
Thanks |
|
| Back to top |
|
 |
arfer Master

Joined: 26 Nov 2004 Posts: 436 Location: Manchester, UK
|
Posted: Sat Apr 04, 2009 6:37 pm Post subject: |
|
|
| Code: |
foreach {name value} [array get array1] {set array2($name) $value}
|
If you call this on numerous occasions, it would be wise first to unset array2 if it exists, otherwise it could continue to contain elements long since defunct in array1.
| Code: |
proc pCopyArray {} {
global array1 array2
if {[array exists array2]} {array unset array2}
foreach {name value} [array get array1] {set array2($name) $value}
return 0
}
|
Alternatively, and simpler I suppose
| Code: |
array set array2 [array get array1]
|
_________________ I must have had nothing to do |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|