mrQWERTY
mrQWERTY

Reputation: 4149

Setting array value as a list

I am having trouble with this snippet of code:

    set rect(1) [list 0 0 $x1 $y1]
    set rect(2) [list $x1 0 $x2 $y1]
    set rect(3) [list $x2 0 $x3 $y1]
    set rect(4) [list 0 $y1 $x1 $y2]
    set rect(5) [list $x1 $y1 $x2 $y2]
    set rect(6) [list $x2 $y1 $x3 $y2]
    set rect(7) [list 0 $y2 $x1 $y3]
    set rect(8) [list $x1 $y2 $x2 $y3]
    set rect(9) [list $x2 $y2 $x3 $y3]

    #iterate thru squares, x for 1, o for 2
    for {set i 1} {$i < 10} {incr i} {
        if {$squares(s$i) == 1} {
            drawX $rect($i) #This part is troubling
        } elseif {$squares(s$i) == 2} {
            .whole.board create oval $rect($i)
        }
    }

proc drawX {x1 y1 x2 y2} {
    .whole.board create line $x1 $y1 $x2 $y2 -width 3 -fill red
    .whole.board create line $x2 $y1 $x1 $y2 -width 3 -fill red
}

Basically, I am getting the error wrong # args: should be "drawX x1 y1 x2 y2. I don't understand whether my function arguments are incorrect or if I am passing the parameter in incorrectly.

Upvotes: 1

Views: 58

Answers (1)

schlenk
schlenk

Reputation: 7247

You do not use the correct number of arguments.

drawX $rect($i)

This is a function call with just one argument. If you want to expand those arguments, you need to use the expand operator on it (8.5+).

drawX {*}$rect($i)

In older versions that can be done with some eval magic.

Upvotes: 2

Related Questions