http://www.perlmonks.org?node_id=193724


in reply to removing elements from an array containing duplicated patterns

You can try this straight-forward solution:

#!/usr/bin/perl -w use strict; # Define patterns to look for, and create a regexp with them. my @patterns = qw(1dlw 1sdh 1cgi 1xnf); my $expr = join("|", map {quotemeta} @patterns); # The source array. my @source = ( '$?/1dlw&', '*%1sdh^?', '@/!1dlw\/', '$1cgi*&', '?@1sdh%&', '~#1xnf$%', ); # Iterate over each element in source, # and store how many times each pattern # is found. my %seen; foreach my $element (@source) { if($element =~ /($expr)/) { $seen{$1}++; } } # ...and remove the patterns found more than once. while(my($pattern, $count) = each %seen) { @source = grep {!/\Q$pattern\E/} @source if $count > 1; } print join("\n", @source), "\n";
-- Ash/asksh <ask@unixmonks.net>