Remove duplicate values from an array in Perl

Home > Search
  by

Let's say you have an array that has duplicate values.

@array = qw(one two one three one);

 

The following Perl script will remove the duplicates.

sub uniq {
  my %seen;
  grep !$seen{$_}++, @_;
}

@unique = uniq(@array);

 

Printing the @unique array will now produce the following.

one two three

 

Credit goes to Greg Hewgill at this Stack Overflow post.



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments