Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: foreach with array elements problem

by OnionKnight (Sexton)
on Oct 22, 2005 at 02:54 UTC ( [id://502162]=note: print w/replies, xml ) Need Help??


in reply to Re: foreach with array elements problem
in thread foreach with array elements problem

Yeah but what's the correct syntax?
  • Comment on Re^2: foreach with array elements problem

Replies are listed 'Best First'.
Re^3: foreach with array elements problem
by donarb (Beadle) on Oct 22, 2005 at 18:48 UTC
    To answer the original question, why is it a syntax error?

    Going back to perlsyn, here's what it says about the variable in a foreach loop:

    If the variable is preceded with the keyword my, then it is lexically scoped, and is therefore visible only within the loop. Otherwise, the variable is implicitly local to the loop and regains its former value upon exiting the loop.

    So even though $array[0] is a scalar value, it cannot exist because there is no local version of @array within the loop. Even if there were a local version of @array within the loop, you would still have to copy those values to the external version of @array from within the loop.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-04-19 14:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found