Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: Array of variables

by Laurent_R (Abbot)
on May 28, 2013 at 21:28 UTC ( #1035736=note: print w/replies, xml ) Need Help??

in reply to Array of variables

I am afraid that talking about references is taking the OP far too much forward at this point. It seems to me that the question is much more basic

To the OP: when you assign the array this way:

@arry = ( $one, $two, $three );

Your are not putting these three variables into the array. You are putting into the array a copy of the current value of these variables at the time you do the assignment. So that, after this assignment, you array does not contain $one, $two and $three, but just their value, i.e. 1, 2 and 3.

If you modify any of the three variables afterwards, this will not modify the elements of the array.

Just to give another simpler example of the same thing:

my $c = 3; my $d = $c; $c = 4; print "$d \n"; # prints 3

On the second line, $d is assigned the value of $c at the time of the assignment, i.e. 3. $d is now a copy of the value of $c at the time of the assignment. Next line, $c is modified to 4. But this has no impact on $d, which has been given the value 3. This is exactly the same thing with your array.

This is very basic programming. If a variable is assigned to another variable, it takes the value of the variable that is current at the time of the assignment. Modifying later the other variable has no impact on the first variable.

You will learn later that it is possible to have the behavior that you were sort of expecting, this is what references are all about, but this is a more advanced topic that you probably don't want to study right now.

Replies are listed 'Best First'.
Re^2: Array of variables
by rtteal (Novice) on May 28, 2013 at 21:49 UTC

    This is a very good explanation! I was having a hard time figuring out what was going on here.

    Thank you all for the help!

Re^2: Array of variables
by Jenda (Abbot) on May 29, 2013 at 11:17 UTC

    It's good to know though, that an object is a reference. This means that if $a is an object and you evaluate $b = $a; then both variables point to the same object and $a->Foo() is equivalent to $b->Foo();. Changing the value of $a doesn't change the value of $b though so if you later evaluate $a = new SomeObject('whatever'); then $a will refer to a different object than $b and $a->Foo() will no longer be equivalent to $b->Foo()!

    Even if you do not use references and complex data structures yourself, it's good to understand this.

    Enoch was right!
    Enjoy the last years of Rome.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1035736]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2017-07-21 11:51 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (321 votes). Check out past polls.