This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

How to copy one array to another

Help for those learning Tcl or writing their own scripts.
Post Reply
w
willyw
Revered One
Posts: 1209
Joined: Thu Jan 15, 2009 12:55 am

How to copy one array to another

Post by willyw »

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
User avatar
arfer
Master
Posts: 436
Joined: Fri Nov 26, 2004 8:45 pm
Location: Manchester, UK

Post by arfer »

Code: Select all

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: Select all

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: Select all

array set array2 [array get array1]
I must have had nothing to do
Post Reply