You mean like

my $array_ref = []; for(1 .. 2) { my $hash_ref = { foo => foo, bar => bar }; push(@{$array_ref}, $hash_ref); } my $hash_ref = $array_ref->[0]; $array_ref = [];

You'll be left with the hash ref, the referenced hash, it's keys and values.

Here's what happens, illustrated!

Initial State: $array_ref $hash_ref +--------------+ +--------------+ | array ref | | hash ref | +--------------+ +--------------+ | refcount = 1 | | refcount = 1 | +--------------+ +--------------------+ +--------------+ | o=====> | array | | o | +--------------+ +--------------------+ +---------- : -+ | refcount = 1 | : +--------------------+ : | element 0 | : | +--------------+ | : | | hash ref | | : | +--------------+ | : | | refcount = 1 | | V | +--------------+ | +--------------+ | | o========> | hash | | +--------------+ | +--------------+ +--------------------+ | refcount = 2 | | element 1 | +--------------+ | +--------------+ | | keys & vals | | | hash ref | | +--------------+ | +--------------+ | | | refcount = 1 | | | +--------------+ | +--------------+ | | o========> | hash | | +--------------+ | +--------------+ +--------------------+ | refcount = 1 | +--------------+ | keys & vals | +--------------+ ##### After the array reference is overwritten: +--------------+ +--------------+ | array ref | | hash ref | +--------------+ +--------------+ | refcount = 1 | | refcount = 1 | +--------------+ +--------------------+ +--------------+ | o | | array | | o | +---------- : -+ +--------------------+ +---------- : -+ : | refcount = 0 | : : +--------------------+ : V | element 0 | : +--------------+ | +--------------+ | : | array | | | hash ref | | : +--------------+ | +--------------+ | : | refcount = 1 | | | refcount = 1 | | V +--------------+ | +--------------+ | +--------------+ | no elements | | | o========> | hash | +--------------+ | +--------------+ | +--------------+ +--------------------+ | refcount = 2 | | element 1 | +--------------+ | +--------------+ | | keys & vals | | | hash ref | | +--------------+ | +--------------+ | | | refcount = 1 | | | +--------------+ | +--------------+ | | o========> | hash | | +--------------+ | +--------------+ +--------------------+ | refcount = 1 | +--------------+ | keys & vals | +--------------+ ##### Automatically, the original array is freed since its refcount == 0. +--------------+ +--------------+ | array ref | | hash ref | +--------------+ +--------------+ | refcount = 1 | | refcount = 1 | +--------------+ +--------------+ | o | | o | +---------- : -+ +---------- : -+ : : : : V : +--------------+ +--------------+ : | array | | hash ref | : +--------------+ +--------------+ : | refcount = 1 | | refcount = 0 | V +--------------+ +--------------+ +--------------+ | no elements | | o========> | hash | +--------------+ +--------------+ +--------------+ | refcount = 2 | +--------------+ +--------------+ | keys & vals | | hash ref | +--------------+ +--------------+ | refcount = 0 | +--------------+ +--------------+ | o========> | hash | +--------------+ +--------------+ | refcount = 1 | +--------------+ | keys & vals | +--------------+ ##### Automatically, the hash refs with refcount == 0 are freed. +--------------+ +--------------+ | array ref | | hash ref | +--------------+ +--------------+ | refcount = 1 | | refcount = 1 | +--------------+ +--------------+ | o | | o | +---------- : -+ +---------- : -+ : : : : V : +--------------+ : | array | : +--------------+ : | refcount = 1 | V +--------------+ +--------------+ | no elements | | hash | +--------------+ +--------------+ | refcount = 1 | +--------------+ | keys & vals | +--------------+ +--------------+ | hash | +--------------+ | refcount = 0 | +--------------+ | keys & vals | +--------------+ ##### Automatically, the hash with refcount == 0 is freed. +--------------+ +--------------+ | array ref | | hash ref | +--------------+ +--------------+ | refcount = 1 | | refcount = 1 | +--------------+ +--------------+ | o | | o | +---------- : -+ +---------- : -+ : : : : V : +--------------+ : | array | : +--------------+ : | refcount = 1 | V +--------------+ +--------------+ | no elements | | hash | +--------------+ +--------------+ | refcount = 1 | +--------------+ | keys & vals | +--------------+

In reply to Re^2: deleteing references by ikegami
in thread deleteing references by jshin

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.