in reply to declaring a common loop variable
Even if I have the same name for the loop variable, the following two $things are different right?
Yes, they are two separate variables lexically scoped to the loops.
My question is, is this considered ok in Perl? or am I to have a seperate loop variable for each of the arrays?
That depends on your definition of "ok" ;-) Techincally, yes, you can do it. I just wouldn't consider it good style. Also, what would be the point of re-using this variable? As you've already seen with your test code, its value will be locally scoped to the foreach loop, so you don't seem to be gaining anything by doing it, and the problem is that the code might get confusing by re-using the same variable.
Update: See also the first few paragraphs of Foreach Loops.
|
|---|