egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to copy one array to another

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
willyw
Revered One


Joined: 15 Jan 2009
Posts: 1175

PostPosted: Sat Apr 04, 2009 3:04 pm    Post subject: How to copy one array to another Reply with quote

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
View user's profile Send private message
arfer
Master


Joined: 26 Nov 2004
Posts: 436
Location: Manchester, UK

PostPosted: Sat Apr 04, 2009 6:37 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
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


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber