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

Re^2: Extracting unique elements from array

by SimonSaysCake (Beadle)
on Dec 12, 2015 at 14:26 UTC ( [id://1150112]=note: print w/replies, xml ) Need Help??


in reply to Re: Extracting unique elements from array
in thread Extracting unique elements from array

Here's a one-liner (if the original order isn't important) - no temp hash var required:
@foo = (1, 1, 1, 2, 3, 3, 3, 4, 5, 5, 6, 7, 8, 8, 9, 10); my @unique = sort {$a <=> $b} keys({map {$_ => 1} @foo}); print join(",", @unique) . "\n";
Two or more arrays? Not a problem.
@foo = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10); @bar = (0, 1, 2, 5, 7, 9, 11, 12, 13, 14); my @unique = sort {$a <=> $b} keys({map {$_ => 1} (@foo, @bar)}); print join(",", @unique) . "\n";
-Simon

Log In?
Username:
Password:

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

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

      No recent polls found