In this example, note how the references remain the same and the array contents are overwritten:

$ perl -wE 'for (0 .. 1) { my @x = $_ == 0 ? qw{a b} : qw{c d e}; say +\@x; say @x; }' ARRAY(0x100a1d40) ab ARRAY(0x100a1d40) cde

When you enter the loop, the array comes into existence and the reference count is set to 1. When you exit the loop, it goes out of scope and no longer exists; the reference count drops back to zero. With the reference count at zero, the reference can be reused on each subsequent iteration.

This seems to emulate what you are referring to in your description.

If you store the array reference in a variable that was created outside of the loop, the reference count is incremented again (i.e. to 2). When you exit the loop, the reference count drops down to 1. As the reference count is not zero, a new reference is created on each iteration. Here's another example showing that:

$ perl -wE 'my %h; for (0 .. 1) { my @x = $_ == 0 ? qw{a b} : qw{c d e +}; $h{$_} = \@x; say \@x; say $h{$_}; say @x; }' ARRAY(0x100a1d10) ARRAY(0x100a1d10) ab ARRAY(0x10053c88) ARRAY(0x10053c88) cde

Here the data is not overwritten: a new array is used each time.

This seems to emulate what is going on in your code.

It's difficult to be sure exactly what's going on here as you've shown insufficent code and no output.

Given that your description and your code appear to be at odds with each other, I wonder whether you updated one without updating the other.

-- Ken


In reply to Re: How to create an anonymous array for lines in a file and store a reference to that array in the hash by kcott
in thread How to create an anonymous array for lines in a file and store a reference to that array in the hash by iphone

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.