Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

GOLF challenge: destroy dup array elements

by George_Sherston (Vicar)
on Sep 06, 2001 at 20:54 UTC ( [id://110639]=perlmeditation: print w/replies, xml ) Need Help??

This was suggested by mothra's interesting reply to my recent post. It's possible to cut out the duplicates from an array with my %uniq; @uniq{@arr} = @arr; @arr = keys %uniq; (thanks to davorg) so long as the array elements are fairly well-behaved critters. But what if they're something odd like hash assignments? What's the slickest way to get rid of duplicate array elements that works whatever elements are in the array?

This one's a bit too rich for my blood - I don't see how to do it without looping through the array, and even then I wouldn't know how to be sure it wouldn't break - but I would love to see how the higher clerics do it.

§ George Sherston

Replies are listed 'Best First'.
Re (tilly) 1: GOLF challenge: destroy dup array elements
by tilly (Archbishop) on Sep 06, 2001 at 23:21 UTC
    Before you can eliminate duplicates, you have to agree on when they are the same. When do you call things the same? This is richly ambiguous. Are two scalars that have the same string the same? How about two hash references to hashes that have identical components? And what do we do with overloaded objects?

    Some idea of the complexity may be understood by saying how many distinct elements @a_list produces:

    my $ref = []; @a_list = ([[], []], [[], []], [$ref, $ref]);
    There are good arguments for 1, 2, and 3...

    The easiest and therefore usual answer is, "They are the same if they yield the same strings in string context." (That would give 3 above.) That is reasonable. And it can be done with 21 characters:

    sub r{ #23456789_123456789_1 my%e;grep!$e{$_}++,@_ }
    To do substantially better than this, what you need to do is decide on a canonical representation. For instance use Storable to turn data into strings, using the appropriate flag for canonical order. The full code for that is..somewhat longer than for above. And will break when you start to throw around closures, etc...
Re: GOLF challenge: destroy dup array elements
by jynx (Priest) on Sep 06, 2001 at 21:00 UTC

      Nope -- it stringifies everything, which I understand to be contrary to specifications. However, it is inspiring:

      sub r{ values%{{map{$_,$_}@_}} }

      This preserves references as references -- does that meet the specs?

      The Sidhekin

        But wouldn't two items collide in this hash if their stringified representations were the same? For instance, wouldn't an object that stringified to "abc" and the actual string "abc" refuse to coexist in that hash.

        I don't see how using values gets around the stringified nature of the keys.

        -Blake

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://110639]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2024-04-25 04:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found