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?
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 ==
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With