See below.

Beau E. Cox wrote:
> Hi -
> 
>> From: R. Joseph Newton [mailto:[EMAIL PROTECTED]]
>> Subject: Re: Removing duplicates from an array
>> 
>> Troy May wrote:
>> 
>>> Hello,
>>> 
>>> What's the best and cleanest way to remove duplicates from an array?  I
>>> have an array that reads entries from a text file and I would like to
>>> remove all the duplicates from it. 
>>> 
>>> Thanks in advance!
>>> 
>>> Troy
>> 
>> I'd say prevention is the best cure.  Check for duplicates as you read.
>> 
> 
> Yes prevention is prob the best way.
> But if you must remove dups anyway:
> 
>   ...@array_with_dups in the input array...
>   my %temp_hash;
>   $temp_hash{$_} = 1 for (@array_with_dups);
>   my $array_without_dups = ();
>   push @array_without_dups, $_ for (keys %temp_hash);
> 
> The downside is that thr array order will be messed up...
> You can sort it: ... for (sort keys ...

Or, if you want to preserve ordering, you could handle it as you read the
file.

my %temp_hash;
my $array_without_dups = ();
while (<file>) {
  chomp;
  if ($temp_hash{$_} == 0) {
    $temp_hash{$_} = 1;
    push(@array_without_dups, $_);
  }
}

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to