I’m trying to create an unique array regardless of its original order and using no module, this’s what I’ve come up with so far:
my @arr = qw(b a a c d g e f);
my %hash;
@hash{@arr}=();
say keys %hash;
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes. Since hash keys are unique, this is one idiomatic way to do it. The number of ways to accomplish the same thing are many.
You may also use a module, such as List::MoreUtils
Output:
Some different ways to dedupe:
The curly braces creates an anonymous hash for
keys, the map statement creates a list of key/value pairs.Same thing, but in subroutine form, and split into two lines. Note that both lists will be in a semi-random order, since hashes are unordered.
The subroutine used by
List::MoreUtilsis equally simple, and perhaps preferable, since it will preserve the order of arguments. It still uses a hash, though.