go ahead... be a heretic | |
PerlMonks |
Re: Strange behavior of iteration while creating Perl/Tk widgets dynamicallyby bliako (Monsignor) |
on Oct 29, 2021 at 08:50 UTC ( [id://11138182]=note: print w/replies, xml ) | Need Help?? |
Your problem can be demonstrated by following simple code:
Inside the sub, $i is evaluated at the time the sub is called in the context/scope of the for-loop which has now terminated, so $i=5+1=6, which is the expected behaviour. A workaround would be to create a sub via eval which will force it to consider $i's value at the time of the eval. But I am not sure if this is an elegant solution:
btw, once you sort this out, you should start using arrays instead of ${"xx$i"}! Update: another solution would be to introduce another variable local to the loop scope and use that:
Question: what's the difference between $i and $j, don't they both have the same scope? bw, bliako
In Section
Seekers of Perl Wisdom
|
|