colemannist has asked for the wisdom of the Perl Monks concerning the following question:
$refarrayptr is a reference to an array containing hash keys. $ref is a reference to a hash containing references to anonymous arrays. Using the x command in the perl debugger, $refarrayptr looks as follows:foreach $ralias (@{$refarrayptr}) { $raliashash = $ralias; $reference = $ref{$raliashash}; $ralias = ${$reference}[0]; }
Once $ralias has been reassigned with the value of the first element of the $reference array, $ralias doesn't have the value of the first element and the first element of $refarrayptr changes from 'key' to the first element of the $reference array as seen below once again in the debug output:DB<43> x $refarrayptr 0 ARRAY(0x9bfef4c) 0 'key' DB<44>
Here is the output of the code:DB<48> x $refarrayptr 0 ARRAY(0x9bfef4c) 0 'a' DB<49>
$ralias is a simple scalar (I think). Why is the assignment to $ralias changing the $refarrayptr array since it is a simple scalar. Thank you./testprob.pl Initial content of ARRAY(0x9ead350) -> key Final content of ARRAY(0x9ead350) -> a
#!/usr/bin/perl @list = qw(a b c d); $reflist = \@list; #$ref{$reflist} = $reflist; $ref{'key'} = $reflist; push(@refarray,keys %ref); $refarrayptr = \@refarray; foreach $ralias (@{$refarrayptr}) { print "Initial content of $refarrayptr -> " . @{$refarrayptr}[0] . + "\n"; $raliashash = $ralias; $reference = $ref{$raliashash}; $ralias = ${$reference}[0]; print "Final content of $refarrayptr -> " . @{$refarrayptr}[0] . " +\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: assignment to scalar changing array content
by sgifford (Prior) on Nov 16, 2005 at 05:18 UTC | |
|
Re: assignment to scalar changing array content
by ysth (Canon) on Nov 16, 2005 at 07:01 UTC | |
by reasonablekeith (Deacon) on Nov 16, 2005 at 10:57 UTC |