on Mar 13, 2007 at 14:40 UTC ( #604547=perlquestion: print w/replies, xml ) Need Help??
OK, maybe im looking in the wrong places or something, I found loads on removing duplicates from arrays but the examples they gave only seemed to work for integers.
One solution is to pop off each element of the array and compare it against every other element remaining. If there's a match, chuck it, otherwise it must be unique so add it to @output. Surely there's a more efficient perlesque way though?
Can someone offer a Perl newbie a hand?!
Also can anyone suggest other good sites I can draw code samples/tutorials/information from?
How can I remove duplicate elements from a list or array?
Your error likely stems from the fact that you're trying to use == as comparison operator, but == is the operator used to check for numeric comparison. If you want to compare two strings for equality as strings, use the eq operator. But it's better to use the method outlined in the FAQ.
A really quick way to do it is to create a hash, using the elements of the array as keys.
You should check the Perl FAQ.
This is similar to liverpole's solution but populates the hash using a hash slice rather than a map. Also, you seem to want the resultant array sorted so I've done that.
This is almost a FAQ.
This sounds a lot like homework, so rather than giving you a solution, consider looking at the solutions that you said worked with integers and change your equality operators to those that compare strings.
And this function preserves the order in the array (which the hash-based solutions don't).