Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Variable in a foreach loop

by shadowsong (Pilgrim)
on Jul 15, 2016 at 11:12 UTC ( [id://1167829]=note: print w/replies, xml ) Need Help??


in reply to Variable in a foreach loop

Hi ravi45722

Just to set you straight - that's what it's supposed to do...

There's a very subtle difference between declaring your iterator inside and outside the loop.

  • If you declare your iterator outside the loop, any value it had will then be restored afterwards
  • If you declare your iterator within the loop a new variable will be created that will exist only within the scope of the loop

My advice would be rename your iterator variables to avoid confusion with lexicals outside your loops...

Cheers,
Shadowsong

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1167829]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-19 13:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found