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

We need to see less code. Focus down on the two lines that are causing your trouble and just enough other lines so that we can reproduce it. Your sample code has far too many unknown entities. Try adding use strict; use warnings; and if it doesn't compile clean (appart from your error) then clean it up.

Are any of the ->put lines relevant to the problem?

I'd guess you are after something like the following (but I can't tell from your code):

use strict; use warnings; use Tk; my @unpaidbills = (['Fred', 1.27], ['Joe', 3.46], ['Francis', 2.22], [ +'Joanne', 5.23]); my @checkboxes; my $main = new MainWindow; foreach my $bill (@unpaidbills) { my $state = 0; # Note we get a new $state each time through my $button = $main->Checkbutton ( -text => "$bill->[0]: $bill->[1]", -variable => \$state ); $button->pack (); $checkboxes[@checkboxes] = [$button, $bill, \$state]; } $main->Button(-text=>'Submit', -command=> [\&doSubmit, \@checkboxes])- +>pack (); MainLoop (); sub doSubmit { my ($checkboxes) = @_; for my $entry (@$checkboxes) { my ($button, $bill, $state) = @$entry; if ($$state) { print "$bill->[0] paid \$$bill->[1]\n"; } else { print "\$$bill->[1] not paid for $bill->[0]\n"; } } }

DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: Array of TK::Checkbuttons not acting appropriately
by mikasue (Friar) on Oct 02, 2006 at 02:31 UTC

    This is the line that is erroring

        if ($checkboxvalue[$t] == 1)

    This is the error

    Use of uninitialized value in numeric eq (==) at BPP/BILL.pm line 148.

    I declare @checkboxvalue = () as such.

    Should I do this also $checkboxvalue$t = 0; ?
    I dont' understand why it is "uninitialized".

    Update:
    It seems that when I click the checkbox the onValue is not being set. So below is my declaration of the Checkbutton

    $checkbox[$t] = $bill_table->Checkbutton(-variable=>\$checkboxvalu +e[$t]);

    Perhaps -variable is not what I should use. I'm going to read more on the Checkbutton options

      I suggest you converge your problem code toward the sample I gave. If your problem remains at the end of that process, post the code and we will help further.

      Your immediate problem is likely to be that you are indexing an element that wasn't present in the array, so it is being created for you and set to undef. It is not clear from your code where $t may be set to anything other than 0, but it seems likely in your "real" code that it is.

      Again I suggest that you reproduce the problem in a small sample application (see my sample as a guide) that we can run and check.


      DWIM is Perl's answer to Gödel