Robin Sheat wrote:
Hey, I have a problem that I know is possible, because I have solved it before. However, the computer died and I lost my solution, and can't work it out again :)

What I want to do is pass a couple of array references into a function
and have it modify the contents of the reference.

What I have is (copied directly from my code, hence the odd seeming names etc):

main perl file:
        $setupPotWeights->([EMAIL PROTECTED],[EMAIL PROTECTED]);
        print "C: Pot Up: "[EMAIL PROTECTED]"   Pot Dn: "[EMAIL PROTECTED]"\n";

the sub:
sub doRandomPotentiation {
    my @potUp = @{$_[0]};
    my @potDn = @{$_[1]};

Because you are passing in references you want to catch them into scalars, then to update the values of the references you dereference them in your push, so...


my ($potUp, $potDn) = @_;

...(operate on these, eg:)
        push @potUp, [$layer,$src,$dst];
...
        push @potDn, [$layer,$src,$dst];
...

In the above statements you are manipulating the local versions of two entirely new arrays, rather than the referenced versions....


push @$potUp, [$layer, $src, $dst];
push @$potDn, [$layer, $src, $dst];

However I am curious if this is really doing what you want, as this adds a new anonymous array reference to the original array rather than adding the three values onto the end of the array, so you may need to switch the square brackets to parentheses, or drop the grouping completely.

}

because I'm only dealing with references, I would expect the stuff that
is added inside the sub to end up in @potUpWeights and @potDnWeights in
the main file, however this isn't happening. A print at the bottom of
the sub tells me the data is going in, but it appears to be a different
reference than the main file is using as the print in it tells me the
arrays are size zero. Thoughts? I know I'm missing something really
basic, but damned if I can put a finger on it.


Of course there is the whole notion of updating the external version of the variable from within the sub in this manner, but I trust you know what you are doing, and that you got $layer, $src, and $dst from acceptable sources....


HTH,

http://danconia.org

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to