Bootstrap FreeKB - Perl (Scripting) - Duplicate elements in an array
Perl (Scripting) - Duplicate elements in an array

Updated:   |  Perl (Scripting) articles

The following script demonstrates how to determine if there are duplicate elements in an array.

#!/usr/bin/perl
use strict;
use warnings;

my @fruits = qw(orange apple orange grape pineapple banana);
my %seen;
foreach my $fruit (@fruits) {
  if ($seen{$fruit}++) {
    print "$fruit has already been seen \n";
  }
}

 

Running this script should return the following.

orange has already been seen
orange has already been seen

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 32442e in the box below so that we can be sure you are a human.