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.

$$var and switch question

Old posts that have not been replied to for several years.
Locked
User avatar
arcane
Master
Posts: 280
Joined: Thu Jan 30, 2003 9:18 am
Location: Germany
Contact:

$$var and switch question

Post by arcane »

hi
got some problem. in my script i have several standard settings which can be overwritten by chan settings.
e.g. i have var vonend(standard), vonend(#mychan) and vonend(#anotherchan). now if vonend(#mychan) exists, the script should use vonend(#mychan), otherwise vonend(standard).
now to prevent an [info exists] every time i need a variable, i wrote a proc which should decide wheter chan setting or standard setting is taken.
here is what i got:

Code: Select all

proc avote:getValue {var value} {

	if {[info exists $var($value)]} {
		return $$var($value)
	} else {
		return $$var(standard)
	}
}
its called with

Code: Select all

avote:getValue "vonend" "#mychan"
but i always get "Tcl error [avote:getValue]: can't read "var(standard)": variable isn't array". normally it should check if vonend(#mychan) exists and then decide whether to use vonend(#mychan) or vonend(standard)
can anyone please correct my code?
and then ive got a question about the switch command.
ive got: if {x=="1" || x=="2"} {
what would that be in a switch command?
aVote page back online!
Check out the most popular voting script for eggdrop bots.

Join the metal tavern!
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Code: Select all

if {[info exists var($value)]} {
also, call globaly the vonend (add a "global vonend" line in the proc) and there is no need of two $ when calling the variable/array
Last edited by caesar on Sun Nov 16, 2003 3:38 pm, edited 1 time in total.
Once the game is over, the king and the pawn go back in the same box.
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

Your code fails because you're working with the local variable "var" and not the target array. Try this:

Code: Select all

proc avote:getValue {array element} {
	upvar 1 $array var
	if {[info exists var($element)]} {
		set var($element)
	} {
		set var(standard)
	}
}
arcane wrote:and then ive got a question about the switch command.
ive got: if {x=="1" || x=="2"} {
what would that be in a switch command?

Code: Select all

switch -- x {
    "1" - "2" {do stuff}
}
Have you ever read "The Manual"?
User avatar
arcane
Master
Posts: 280
Joined: Thu Jan 30, 2003 9:18 am
Location: Germany
Contact:

Post by arcane »

caesar wrote:

Code: Select all

if {[info exists var($value)]} {
wouldnt this check for var? it should check if vonend($value) exists.
caesar wrote: also, call globaly the vonend (add a "global vonend" line in the proc) and there is no need of two $ when calling the variable/array
sorry, forgot that. of course i have this global line with all possible values for "var".

@user: yes, this seems more correct to me :) thx and ill come back if you told me nonsense :mrgreen:
aVote page back online!
Check out the most popular voting script for eggdrop bots.

Join the metal tavern!
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

I'm shure he didn't :P
Once the game is over, the king and the pawn go back in the same box.
User avatar
arcane
Master
Posts: 280
Joined: Thu Jan 30, 2003 9:18 am
Location: Germany
Contact:

Post by arcane »

yeah... seems good work :)
aVote page back online!
Check out the most popular voting script for eggdrop bots.

Join the metal tavern!
Locked