On Apr 21, Bob Showalter said:

>Peterson, Darren - Contractor.Westar wrote:
>> I'd like to remove duplicate values from an array to leave it with
>> only unique values.  For instance, if an array contains
>> (1,2,3,1,4,2,5) as values, I'd like to clean out the extra 1 and 2 to
>> leave the values as (1,2,3,4,5).  Does perl have an array command to
>> do that, or must the process be purely manual?
>
>The canonical response is to use a temporary hash along the lines of:
>
>   @arr = do { my %seen; grep !$seen{$_}++, @arr };
>
>There's a Tie::Array::Unique module on CPAN which gives you a lot more
>flexibility in handling this kind of thing.

I agree (says the author of the module).  It maintains the uniqueness
throughout the life of the array, so you don't have to keep removing
duplicates every time you want to add one -- it does it for you.

>See also the FAQ article
>
>perldoc -q 'How can I remove duplicate elements from a list or array?'

Perhaps I should patch the Perl FAQ to include my module...

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
CPAN ID: PINYAN    [Need a programmer?  If you like my work, let me know.]
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to