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.

array help please

Old posts that have not been replied to for several years.
Locked
User avatar
droolin
Halfop
Posts: 64
Joined: Thu Jul 24, 2003 9:07 pm
Contact:

array help please

Post by droolin »

Ive written a script that performs various security functions on a network i am part of. I've just made a modification where I access various pre-defined channels with the eggie for various things. I have this list set up in a variable as such.

Code: Select all

set     cycle_channels   {
        "#channel1"
        "#channel2"
        "#channel3"
}
The code that I use with this list works fine, but what I want to do is add 2 other vraibales that will be associated with each channel. Something like:

Code: Select all

array set     cycle_channels   {
        "#channel1"   0 0
        "#channel2"    0 0
        "#channel3"   0 0
}
The second value would be a counter, and the third would contain a time stamp. I
think
that I have defined an array properly.
Would I incriment the counter of the first channel like this????

Code: Select all

inc cycle_channels(1,1)
Or would you use arrayget, to set a variable. incrament the value, then array set? And shoot, Im not even sure what the syntex on this would be.
God, I hate asking stupid questions.
;o(((

droolin
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

Hi,

Unfortunately tcl doesn't support multi-dimensional arrays. You can technically have an array element like somearray(1,1), but that is actually a 1 dimensional array with a key of "1,1" (ie. the comma is part of the key name).

What you might want to do is store a list for each element of the array:

set somearray(#chan1) [list "hi there" 1234 -5]

Now you have 3 values stored in that array element, in a list. To access them, you'd go:

set var1 [lindex $somearray(#chan1) 0] (result "hi there")
set var2 [lindex $somearray(#chan1) 1] (result 1234)
etc

The crappy part is when you want to update one of the vars:

set somearray(#chan1) [lreplace $somearray(#chan1) 1 1 2345]
(now the list is {"hi there", 2345, -5})

see http://www.tcl.tk/man/tcl8.3/TclCmd/contents.htm for more info
User avatar
droolin
Halfop
Posts: 64
Joined: Thu Jul 24, 2003 9:07 pm
Contact:

hmmmm

Post by droolin »

Ok, due to I want to keep all code as simple and understandable as possible. I will set up 2 more variables that will contain the added values. I want as little manual configuration of the variables as possible. In other words, I would only have to update the cycle_channels variable if any new channels would need to be added/deleted.
I was going to ask another question on this, but I think I know the answer. I will set up two new variables that will be list's that will contain my counts and time stamps. At initial time of eggdrop loading, I will run an initialization routine to insure that numeric values are in each occurance that there is a matching entry in cycle_channels. This way, they will not have to be manually modified. If there are 5 entries in cycle_channels, my initialization routine will update 5 occurances in the new variables... Less maintance, and achieves the same results.
Thank you for your help, because I was spending hours searching the net on how to do this. I wanted to do it as clean as possible, but just wasnt sure how.

droolin

[/code]
Locked