in reply to Understanding Perl context
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!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Understanding Perl context (removing duplicates from array)
by AnomalousMonk (Archbishop) on Apr 19, 2015 at 13:17 UTC | |
by LanX (Saint) on Apr 19, 2015 at 13:29 UTC | |
by AnomalousMonk (Archbishop) on Apr 19, 2015 at 14:27 UTC | |
by LanX (Saint) on Apr 19, 2015 at 14:47 UTC |