Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: How can I display a unique array if it contains some repeated elements

by dvergin (Monsignor)
on Apr 28, 2001 at 10:10 UTC ( [id://76354]=note: print w/replies, xml ) Need Help??


in reply to How can I display a unique array if it contains some repeated elements

If you mean, "How can I display an array of the unique elements of an array that contains some repeated elements", this may serve your purpose:
my @ary = qw(a b c d d e b d); my %hsh; undef @hsh{@ary}; my @ary2 = keys %hsh; print "@ary2\n";
The key here is:  undef @hsh{@ary};  which is a hash slice. The hash slice is an lvalue (that is, it is assignable). The undef is associative for each element of the slice. The effect of this is to establish a key in the hash corresponding to each array element. Except that duplicate keys just write over each other. Each key has a corresponding value of undef. Then, on the next line we just extract the (now unique) keys into a new array.

Replies are listed 'Best First'.
Re: Answer: How can I display a unique array if it contains some repeated elements
by knobunc (Pilgrim) on Apr 30, 2001 at 18:02 UTC

    This is fine for some cases, but it does not preserve the ordering of the elements in the array. The origial poster did not indicate if they cared. Anyway, as IO pointed out, the node How can I extract just the unique elements of an array? discusses several methods for dealing with this depending upon the data that you are dealing with.

    -ben

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2025-06-23 03:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.