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.

Question on arrays

Old posts that have not been replied to for several years.
Locked
A
Alukor

Question on arrays

Post by Alukor »

Hello,

I am creating a small script that is compatiable with multi channels and I am using setudef commands to set variables so I can give each channel its own variable via .chanset. I am trying to create a proc that when executed, it will delete certain array names (not entire array) and my question is how can I make it so the script find a certain value in array and return with array name so I can delete the array name.

ie.

set rchan($nick) $chan

I need $nick, but I only know $chan in this proc so I want to be able to find it and come back with $nick then delete it from there. Can it be done?

Thanks.
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

Something like this?

Code: Select all

foreach {key value} [array get array] {
	if {[string equal $chan $value]} {unset array($key)}
}
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Actualy is array unset array $key
Once the game is over, the king and the pawn go back in the same box.
User avatar
strikelight
Owner
Posts: 708
Joined: Mon Oct 07, 2002 10:39 am
Contact:

Post by strikelight »

caesar wrote:Actualy is array unset array $key
unset array($key) is valid, and is more compatible with lower TCL versions.
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

Very true.

However, you should then use your own compatability function.

A simple "unset" of an array key, will result in memory not being freed unless the whole array is freed later.
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

ppslim wrote:A simple "unset" of an array key, will result in memory not being freed unless the whole array is freed later.
This is false.

No matter what method you use to unset single elements from an array, memory will be freed, but the array itself will continue to exist and occupy the variable name in the interpreter.
As far as I know, 'array unset' with a pattern is the same as doing multiple calls to 'unset' (if the pattern matches more element names)

As usual, my explanations suck...hope you get the idea :)
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

caesar wrote:Actualy is array unset array $key
For this to work like 'unset' you'd have to escape the glob matching chars.
User avatar
strikelight
Owner
Posts: 708
Joined: Mon Oct 07, 2002 10:39 am
Contact:

Post by strikelight »

ppslim wrote:Very true.

However, you should then use your own compatability function.

A simple "unset" of an array key, will result in memory not being freed unless the whole array is freed later.
If I understand you correctly, you are worried about the array existing even if it has no elements?

If this is true, the same can be held true for using array unset array key as well.
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

I am trying to look now, however, I thought that array key names are stored elsewhere.

This is the memory ocupied, and only array unset will free this at the same time.
Locked