aral has asked for the wisdom of the Perl Monks concerning the following question:
G'day. I feel terribly stupid today. Been out of touch with my more complicated Perl project for half a year, and now I cannot wrap my brain around hash and array references anymore.
I want to be able to recursively copy a hash of arrays of scalars (manually, so I can make adjustments on certain nodes). But I fail in creating writeable references to all types (hash, array, scalar) to pass to my recursion routine. A short example with my error looks like this:
my %testHash; # initialize hash my $hashRef = \%testHash; # get a reference to that # get a reference to an element 'a' inside the hash (that does not +yet exist) my $destref = \${$hashRef->{'a'}}; # <- problematic line # assign a value to the hash element 'a' $destref = "value"; # verify the result print $hashRef->{'a'} . "\n"; # outputs "SCALAR(0xaddress)"
In essence, I do not even know whether the "problematic line" is able to create a valid reference, because element 'a' does not exist at the time. How do I get a $destref that I could also pass to a function, and when modifying it's value, accomplish a change of $hashRef->{'a'}?
Thanks in advance for any help. I have done this before and can't get my head around my own code at the moment. I hope I am making sense :/
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: recursively building a hash of arrays of scalars
by choroba (Cardinal) on Sep 30, 2015 at 16:16 UTC | |
by aral (Acolyte) on Oct 02, 2015 at 08:06 UTC | |
by choroba (Cardinal) on Oct 02, 2015 at 08:31 UTC | |
|
Re: recursively building a hash of arrays of scalars
by Athanasius (Cardinal) on Sep 30, 2015 at 16:39 UTC | |
|
Re: recursively building a hash of arrays of scalars
by Laurent_R (Canon) on Sep 30, 2015 at 17:13 UTC |