how to avoid pushing duplicate values into an array in Perl
i need to add unique elements in an array from inputs which contains several duplicate values.
You simply need to use hash like this:
This will automatically overwrite duplicate keys. When you need to print it, simply use:
If you need to sort keys, use
|
|||
|
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 |
|||||
|
by using ~~ we can minimum perl version is 5.10.1
|
|||
|
List::MoreUtils
uniq function if you are not opposed to the CPAN. – squiguy Apr 9 '13 at 6:41