PatGro has asked for the wisdom of the Perl Monks concerning the following question:
Hi all,
It is my first post here. I have trouble understanding the difference between both version of my code. I am using tk for my gui. I am building an array of controls within a loop and, when I use the for loop construct, the code isn't working as expected.
When this code execute, it always print Test 10 If I change only the for loop for foreach my $i (0..$self->{NUM_OF_LANE_MAX}-1) { then the ID of the checkbox clicked is print, i.e. if I click on the 4th checkbox, I get Test 4 As far as I understand, there should not be any differences between the two constructs. In my case, I don't understand the difference between the two codes that makes them behave differently.my @tx_grid_frame; my @outputEnaLpbkBut; for (my $i = 0; $i < $self->{NUM_OF_LANE_MAX}; ++$i){ $tx_grid_frame[$i+1][1] = $quick_tx_right_frm->Frame(-relief=>'gro +ove', -bd=>1)->grid(-row => $i+1, -column => 1, -columnspan => 1, -r +owspan => 1, -sticky => 'nsew'); #Output enable when loopback $outputEnaLpbkBut[$i] = $self->{W}->Add_Chk($tx_grid_frame[$i+1][ +1], '', 'top'); $outputEnaLpbkBut[$i] -> configure(-variable=> \${$self->{OUTPUT_E +NA_LPBK}}[$i], -command => sub{ print "Test $i \n"; }); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Problem understanding why for loop won't work when foreach is working
by BrowserUk (Patriarch) on Aug 13, 2013 at 14:56 UTC | |
|
Re: Problem understanding why for loop won't work when foreach is working
by choroba (Cardinal) on Aug 13, 2013 at 14:57 UTC | |
|
Re: Problem understanding why for loop won't work when foreach is working
by PatGro (Novice) on Aug 13, 2013 at 15:06 UTC | |
by Athanasius (Archbishop) on Aug 13, 2013 at 16:13 UTC |