in reply to Re^2: reset checkbutton value
in thread reset checkbutton value

Where you mostly went wrong, I think, was in only pushing to @Sel and not popping from it. In other words, if you clicked on a checkbox, you'd push its value to @Sel; and if you clicked it again to deselect it, you'd just push again. The list would grow longer and longer, and things would not work as intended.

Meanwhile, this:

-command => sub{push(@Sel, $n)} && sub{ $Selected[$i] = defined $Selec +ted[$i] ? undef : $n;}

most likely will not do what you may think; although you can use the && operator on anonymous subroutine references (they're scalars, after all), they'll always be a true value, and the operator will return the last value evaluated, in this case the second reference (see C style Logical And in perlop). So the above line is really equivalent to

-command => sub{ $Selected[$i] = defined $Selected[$i] ? undef : $n;}

Now, this IS fairly close to what I wrote, of course, but:

I got around the latter by introducing a new variable inside the loop that holds the current value of $i; this goes out out of scope at the end of the loop body, so in the next loop iteration, a new variable with the same name is created, and each anonymous subroutine sees a different variable, one that keeps the right value of $i. (It's worth noting that even though the variables go out of scope, their duration does not end, as they're still visible to the anonymous subroutines.)

This is perhaps a little difficult to wrap your head around at first. For more information on scope and duration, I'd recommend Mark Dominus's Higher Order Perl (available as a free PDF here, or a dead-tree edition here), pp. 71-76.