Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: removing elements from an array containing duplicated patterns

by ash (Monk)
on Aug 29, 2002 at 10:33 UTC ( [id://193724]=note: print w/replies, xml ) Need Help??


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>
  • Comment on Re: removing elements from an array containing duplicated patterns
  • Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://193724]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-25 15:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found