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.

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"; }); }
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.
Thank you for your insights,
P.

In reply to Problem understanding why for loop won't work when foreach is working by PatGro

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.