In Perl, {…} creates a hash reference, not an array – those are initialized using a list (…). You should consider to use strict; use warnings; as a safety net.
my @array = ('val1','val2','val3','val4','val1');
my %seen;
my $element;
foreach $element (@array){
$seen{$element}++
}
foreach my $val (keys %seen){
print "No of repetition of $val = $seen{$val} times\n" if ($seen{$val} > 1);
}
Sign up to request clarification or add additional context in comments.
Comments
2
my @array = ('val1','val2','val3','val4','val1');
my %seen;
$seen{$_}++ for @array;
print "No of repetition of $_ = $seen{$_} times\n"
for grep $seen{$_} >1, sort keys %seen;
{…}creates a hash reference, not an array – those are initialized using a list(…). You should consider touse strict; use warnings;as a safety net.