Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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

by Anonymous Monk
on Aug 14, 2003 at 21:39 UTC ( [id://284025]=note: print w/replies, xml ) Need Help??


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

a sub to get both unique and not:
@array1 = (1,1,2,3,3,4); #unique print unique(@array1); #not print unique(\@array1); sub unique{ my (%seen, $array); ($array)=@_; if(@$array){return grep{$seen{$_} ++} @$array;} else{return grep{ ! $seen{$_} ++} @_;} }
  • Comment on Re: How can I display a unique array if it contains some repeated elements
  • Download Code

Replies are listed 'Best First'.
Re: Answer: How can I display a unique array if it contains some repeated elements
by halley (Prior) on Aug 14, 2003 at 21:47 UTC
    Okay, that's a really bad idea. You're naming a sub suggestively and then offering two OPPOSITE functionalities.

    You're expecting the caller to choose the desired action with an UNRELATED and NON-MNEMONIC distinction like ref($_[0]) vs not ref($_[0]).

    What if you send a plain @array1 and the zeroth element is an array ref? You might want to get a unique set of array refs, but this function's not going to help there.

    --
    [ e d @ h a l l e y . c c ]

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2025-05-25 01:01 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.