Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

In loops like this, Perl does some magic that other popular programming languages generally do not. For folks who come from other languages or are not familiar with this magic, it can be surprising, and often in negative ways.

You might normally think that the value of each element in the list is copied to the loop variable ($item in this case). In fact, the loop variable becomes a synonym for the elements of the list. This means that if you modify the loop variable, it will modify the object inside the list to which it is currently referencing.

To be even more specific, this only applies when the list element currently being processed is an "lvalue" (fancy language for "something that can be modified"). So, if the code is

foreach my $item ($a, 2, $c) { $item = 1; }
then $a and $c will be set to 1, but the constant "2" will be copied into $item, $item will be changed to 1 and the constant "2" will be left alone.

This is why your first code sample works the way that you want it to. You have listed the actual variables you want to modify inside the foreach loop.

In your second sample, you are first copying the values in your variables into a new array and using that array in the loop. Now, you're modifying the elements of the array inside the loop and not the original variables.

Another place where this type of synonym behavior works is in calls to subroutines. The elements of @_ are actually referencing the variables of the caller's parameters (where they are lvalues). Most of the time we tend to copy @_ elements into local variables; modifying those will not modify the caller's data.

But I ramble.

-- Eric Hammond


In reply to Re: array confusion by esh
in thread array confusion by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (1)
As of 2022-01-29 06:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (74 votes). Check out past polls.

    Notices?