0

Given Data

array set color {a red b green c blue}
set keylist [array names $color]
pointlist  {{1 2 3 4} {5 6 7 8} {9 10 11 12} {13 14 15 16}}

I get a new list after running a procedure.

 newlist  {b b b b}     # Note: length of newlist is same as length of pointlist

Question is : when i run a loop these values of b should be substituted, for example in this green.

for {set i 0} { $i < [llength $pointlist]} {incr i } {
    lassign [lindex $pointlist $i] x1 y1 x2 y2
    APICommand -points "$x1,$y1,$x2,$y2" -color $color($keylist)
}

Now this $color($keylist) is not getting me correct result and errors out saying : can't read "color(red blue green)": no such element in array

Instead I want the first set of 4 points to get the green color which is the value of b. Similarly next set of 4 points from pointlist should also get green as its value is also b.

 #So, after substitution it should look like APIcommand -points 1,2,3,4 -color green ..... so on and so forth

Note this will not always be b , it just happens to be in this case.

Need solution asap. Thanks in advance.

2 Answers 2

1

Sounds like you need $color([lindex $newlist $i])

Sign up to request clarification or add additional context in comments.

1 Comment

I was trying to use a foreach loop inside the for loop and then use -color [lindex $color($newlist) $i] and that is why it was giving me error. but using the array name first and then the index of keylist solved my problem. Thanks again !! (It's exactly what I was looking for )
1

When iterating across a list, the foreach command is useful. It can also be used to iterate over multiple corresponding lists. Assuming newlist holds the colors that correspond to the points, I might try something like (usual warnings about untested code):

foreach point $pointlist new $newlist {
    APICommand -points [join $point ,] -color $color($new)
}

1 Comment

We dont have to calculate foreach point, we have to do this for group of 4 points. means 4 points corresponds to one color

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.