D
Dan Otterburn
I have an array of a number of items, some of which are duplicates. I
need to "de-dupe" the array, keeping the item with the lowest index.
my @fruits = qw(
apple
apple
pear
banana
pear
apple
banana
plum
plum
apple
plum
peach
kiwi
pear
plum
banana
cherry
);
The "apple" I want is $fruits[0], the "pear" $fruits[2] etc...
My current solution is:
my @fruits_deduped;
while (my $fruit = pop @fruits) {
next if grep { $_ eq $fruit } @fruits;
push @fruits_deduped, $fruit;
}
@fruits = reverse @fruits_deduped;
This seems to be a lot of work, is there a better way to do this?
need to "de-dupe" the array, keeping the item with the lowest index.
my @fruits = qw(
apple
apple
pear
banana
pear
apple
banana
plum
plum
apple
plum
peach
kiwi
pear
plum
banana
cherry
);
The "apple" I want is $fruits[0], the "pear" $fruits[2] etc...
My current solution is:
my @fruits_deduped;
while (my $fruit = pop @fruits) {
next if grep { $_ eq $fruit } @fruits;
push @fruits_deduped, $fruit;
}
@fruits = reverse @fruits_deduped;
This seems to be a lot of work, is there a better way to do this?