C:\Users\pritesh\textfiles>perl undef.pl
Before: $VAR1 = {};
After: $VAR1 = {
'1' => undef,
'3' => undef,
'54' => undef,
'2' => undef,
'4' => undef,
'45' => undef,
'55' => undef
};
So like you said, 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. Now, keys gotta be unique, so it's going to remove the duplicates, and just keep the unique keys.
Boy, I feel like Neo when he finally "gets" the Matrix :D
On a serious note, Data::Dumper should be called Data::Enlightenment :)
| [reply] [d/l] |
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:
- It is the last element of the hash slice which is explicitly set to undef.
- If an element in @array is already present as a key in %unique, the corresponding value is not overwritten (unless that element happens to be the last in @array, in which case it is set to undef).
But again, it is autovivification that is responsible for the magic that makes this a useful idiom.
Hope that helps,
Update: Improved wording.
| [reply] [d/l] [select] |
Hi Athanasius,
That is some pretty deep stuff sir. Thank you so much for taking time to write in such a detail.
This is going to take some time for me to grasp, but one bite at a time is how I'm gonna go about it.
Thank you once again. Indeed, Perl and Monks like yourself are awesome.
| [reply] |