Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Variable in a foreach loop

by shadowsong (Monk)
on Jul 15, 2016 at 11:12 UTC ( #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
Node Status?
node history
Node Type: note [id://1167829]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2019-05-22 04:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you enjoy 3D movies?



    Results (138 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!