Perl count for matching strings in an array -


i have array filled strings. want check if 1 specific string more once in array , print error warning.

i used true method in list::moreutils: count matches. in array have strings, have substrings same other string in same array.
if check if same string more once in array error warning though there maybe string same substring. tried fix problem adding string length pattern (so string , length have equal error message pops up), not work either.
code looks this:

use strict; use warnings; use list::moreutils 'true';  @list = ("one", "two", "three", "onefour", "one");  foreach $f (@list) {            $length = length($f);         $count = true { $length && "$f"} @list;             if($count > 1) {                     print "error with: ", $f, " counted ", $count, " times!\n";                 }        $count = 0;     } 

with code not error warning @ all, though "one" twice in array. if not include length pattern of true method, string "one" counted 3 times.

i wouldn't use true - looks you're trying 'pick out' duplicates, , don't care substrings.

my %seen; $seen{$_}++ @list;  print grep { $seen{$_} > 1 } @list;  

so replicate test:

my %count_of; $count_of{$_}++ @list;   foreach $duplicate (  grep { $count_of{$_} > 1 } @list ) {     print "error: $duplicate seen $count_of{$duplicate} time\n"; } 

Comments