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.

${type}game($number) - reading

Old posts that have not been replied to for several years.
Locked
d
dFt^Nut

${type}game($number) - reading

Post by dFt^Nut »

Code: Select all

set i 0
 lappend game ""
 set id [array startsearch alias]
 while {[array anymore alias $id]} {
        set item [array nextelement alias $id]
        set ding [split $item #]
        set type [lindex $ding 0]
        set number [lindex $ding 1]
        set ${type}game($number) "$alias($item)"

 if {$type == "jkja"} { putchan $chan "debug1: ${type}game($number)" }

        lappend num $i

        lappend output $type

        if {[lsearch -all $game $type] >= 1} {
                continue
        } else {
                lappend game $type
        }
        incr i
 }
The problem is that in the 'if {$type == "jkja"}' line the output of the putchan is jkjagame(1) rather then what that value holds, if i change ${type}game($number) into $jkjagame($number) it works and displays just that what the array value holds...

What am i doing wrong here?
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

A couple ways to do it. Maybe the easiest way is to use the set command. It returns the value of the variable you give it, so you can do more advanced substitution using it:

set num [set ${type}game($number)]

Now $num = $jasdfgame($number)
d
dFt^Nut

Post by dFt^Nut »

woohoo! now it works :mrgreen:

Could you give me the alternative means to do this aswell?

thanks for the help, been bashing my head on this for a few days now, trying to work around it with no luck :cry:
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

Another way to do it is with upvar, especially if it's a global variable. You create a link to the array and then manipulate it as if it were the other variable.

proc doit {} {
set type green
set var hunger
upvar #0 ${type}dragon link
set link($var) "sheep"
}

now global greendragon(hunger) = sheep

The same thing works for local variables, using 0 instead of #0 in upvar.

You can also do tricks like the set one with subst and eval:

set type green
set a [subst $${type}dragon(hunger)]
eval set b $${type}dragon(hunger)

now $a = $b = sheep

You are entering the dark side of tcl scripting. Don't let its power draw you in!
d
dFt^Nut

Post by dFt^Nut »

I'm afraid i'm allready in it :evil: :mrgreen:

That what you are tying to do with $${x}y($z) gives errors with me, or is this what you do only valid with the subst command?
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

It's only with subst and eval. And to use $z you probably have to do \$z, but I can't check right now.
Locked