Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid pushing duplicate values into a Perl array

Tags:

arrays

perl

I need to add unique elements in an array from inputs which contain several duplicate values.

How do I avoid pushing duplicate values into an Perl array?

like image 478
Subhash Avatar asked Apr 09 '13 06:04

Subhash


1 Answers

push(@yourarray, $yourvalue) unless grep{$_ == $yourvalue} @yourarray;

This checks if the value is present in the array before pushing. If the value is not present it will be pushed.

If the value is not numeric you should use eq instead of ==.

like image 87
Demnogonis Avatar answered Oct 07 '22 16:10

Demnogonis