The first @unordered approach is using a hash slice, and since hash keys are unique, duplicates are overwritten. But the original order is lost, since keys returns in random order.
The second @ordered approach rejects duplicates from copying, hence @ordered preserves the order of the first occurrences. This is done with %seen which postincrements the value per element
if( not $seen{$element}++ )
Only 0 is false, hence repeated (i.e. "unseen") elements are filtered out.
PS: As a side note:
A hash slice initializing undef values is better written as @unordered{@array}=() , assigning undef to the first element is misleading!
Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!
In reply to Re: Understanding Perl context (removing duplicates from array)
by LanX
in thread Understanding Perl context
by shankonit
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |