Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: removing elements from an array containing duplicated patterns

by ash (Monk)
on Aug 29, 2002 at 10:33 UTC ( #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 having an uproarious good time at the Monastery: (4)
As of 2023-12-11 02:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?











    Results (41 votes). Check out past polls.

    Notices?