Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

About this behavior

by chumley (Sexton)
on Apr 16, 2001 at 05:03 UTC ( #72724=note: print w/ replies, xml ) Need Help??


in reply to Re: NEWBIE Brain Teaser
in thread NEWBIE Brain Teaser

Thanks for the explanation. I've learned something new about Perl - which isn't too difficult for me!

I'm just wondering why it's set up this way? Is there an overall principle I can apply to lists, arrays, scalars, hashes, etc., that will help me understand behavior like this?

Or perhaps I should go back and finish reading "Learning Perl" instead of picking up little bits here and there as I need them. :-\

Chumley


Comment on About this behavior
Re: About this behavior
by Macphisto (Hermit) on Apr 16, 2001 at 05:45 UTC
    Well, and someone please correct me if I mislead on this post,

    If you think about the code:
    foreach $each (@list) {
    $each *= 2;
    $newlist$i = $each;
    ++$i;
    }


    From Programming Perl: Note that the loop variable becomes a reference to the element itself, rather than a copy of the element. Hence, modifying the loop variable will modify the original array.

    The snippet loops through the contents of @list and as each cell gets looked at, it is assigned to $each. $each is then modified by multiplying by 2. Modifying $each, modifies the value in the shell, due to the way foreach is constructed in perl. I believe that is all correct. If I have made any mistakes ( other than grammar, and spelling ), someone please tell me.

    Macphisto the I.T. Ninja

    Everyone has their demons....

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2014-07-11 03:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (217 votes), past polls