The following snippet of perl code behaves in a manner that I do not understand. I realize there is another approach but I want to learn what perl is doing in this snippet (The entire test program is at the end of the post) :
foreach $ralias (@{$refarrayptr}) { $raliashash = $ralias; $reference = $ref{$raliashash}; $ralias = ${$reference}[0]; }
$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:
DB<43> x $refarrayptr 0 ARRAY(0x9bfef4c) 0 'key' DB<44>
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<48> x $refarrayptr 0 ARRAY(0x9bfef4c) 0 'a' DB<49>
Here is the output of the code:
./testprob.pl Initial content of ARRAY(0x9ead350) -> key Final content of ARRAY(0x9ead350) -> a
$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
#!/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"; }

In reply to assignment to scalar changing array content by colemannist

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.