in reply to Re: Array of TK::Checkbuttons not acting appropriately
in thread Array of TK::Checkbuttons not acting appropriately

The problem is with the closure (the anonymous sub{...}</> that -command is) and the scope of $t.  Put a <c>warn $t in the sub{} to see what it's doing -- you'll see that it's always 3 which is the value of t after the foreach my $f (@foo) ... Here are two different approaches to solving it:

Here, you can use a temp value to hold the current value of $t. Since $i is scoped inside the for loop, it is different for each closure that is made.
my $i=$t; $bill_table->Button(-text=>'Submit', -command=>sub{if ($checkboxvalue[$i] ==1){prin +t $f}}, )->pack;
Alternatively, use the form of Tk::callback that lets you provide arguments to a sub. Here our anonymous sub takes two args -- a reference to a scalar which is the checkbox value, and a scalar which is the text of the checkbox.
$bill_table->Button(-text=>'Submit', -command=>[ sub{ my $valref = shift; my $valname=shift; if ($$valref ==1){print $valname} }, \$checkboxvalue[$t], $f, ], )->pack;
Now, there's the issue of your Use of uninitialized value in numeric eq (==) .. That is because the checkbox values are either 1 or undef, so when you check <thecheckboxvalue>==1 it'll do either 1==1 (ok) or undef==1 (warning).
To solve, simply do one of these (probably the first? not sure that being exactly 1 is important, as long as it's true:
if( $foo ){ print $f } if( defined $foo ){ print $f } if( $foo && $foo==1){ print $f } if( defined $foo && $foo==1){ print $f }

Replies are listed 'Best First'.
Re^3: Array of TK::Checkbuttons not acting appropriately
by rcseege (Pilgrim) on Oct 02, 2006 at 14:24 UTC
    That is because the checkbox values are either 1 or undef

    I'm a little puzzled. Checkbutton values are supposed to be 1 or 0, defaulting to 0, though I can't argue with what I'm seeing. Try selecting, then deselecting a Checkbutton using mikasue's last example. When you press the Submit button for that row now, you shouldn't see the "Use of unitialized..." message, because now the value is 0 instead of undef. I'm going to have to take another look at why my second and third examples didn't behave this way.

    Having said that, I agree that your first condition:  if ($foo) { ... } is best for this.

    Rob
Re^3: Array of TK::Checkbuttons not acting appropriately
by mikasue (Friar) on Oct 02, 2006 at 13:23 UTC
    THANK YOU davidrw!!!!