Hello pritesh,

the undef @unique{@array} basically does the magical thing of taking all the the elements in the @array, setting them as a key and "undef" the value.

Yes, and in this case the magic is performed by an important Perl feature called autovivification, which Wikipedia explains as follows:

In the Perl programming language, autovivification is the automatic creation of new arrays and hashes as required every time an undefined value is dereferenced. Perl autovivification allows a programmer to refer to a structured variable, and arbitrary sub-elements of that structured variable, without expressly declaring the existence of the variable and its complete structure beforehand.

Here are some references:

If you say:

@unique{ @array } = 'X';

what actually happens is that the first element of the hash slice is set to 'X', and all the remaining elements in the hash slice are set to undef (because 'X' is treated as though it were ('X'), i.e., a list with one element; that element is assigned to the first element on the left-hand side of the assignment, and, since there are no more elements in the right-hand-side list, undef is assigned to the remaining elements on the left-hand side.) The values corresponding to the keys already present in %unique but not in @array remain unaffected. But the values corresponding to keys present in both %unique and @array are overwritten.

Now, if any elements in @array are not already present as keys in %unique, the assignment turns these elements of the hash slice into lvalues. (An lvalue is a value that can be assigned-to, that is, that can appear on the left-hand side of an assignment.) And that’s where autovivification comes in: if these lvalues don’t already exist, Perl creates them for you.

Note that the syntax:

undef @unique{ @array };

produces a similar result, but there are two differences:

But again, it is autovivification that is responsible for the magic that makes this a useful idiom.

Hope that helps,

Update: Improved wording.

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re^3: What does undef do when I say @hashslice{@array} = undef by Athanasius
in thread What does undef do when I say @hashslice{@array} = undef by pritesh

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.