Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: removing elements from an array containing duplicated patterns

by Arien (Pilgrim)
on Aug 29, 2002 at 11:08 UTC ( [id://193730]=note: print w/replies, xml ) Need Help??


in reply to removing elements from an array containing duplicated patterns

Another way to do it:

#!/usr/bin/perl use strict; use warnings; my @data = ( '$?/1dlw&', '*%1sdh^?', '@/!1dlw\\/', '$£1cgi*&', '?@1sdh%&', '~#1xnf$%' ); my @patterns = qw/1dlw 1sdh 1cgi 1xnf/; my $re = join "|", map quotemeta, @patterns; my %seen; for (@data) { next unless /($re)/; # shouldn't happen, presumably if (defined $seen{$1}) { push @{ $seen{$1} }, $_; } else { $seen{$1} = [ $_ ]; } } my @unique = map { @{ $seen{$_} } == 1 ? $seen{$_}->[0] : () } keys %seen;

Edit: Or another way...

#!/usr/bin/perl use strict; use warnings; my @data = ( '$?/1dlw&', '*%1sdh^?', '@/!1dlw\\/', '$£1cgi*&', '?@1sdh%&', '~#1xnf$%' ); my @patterns = qw/1dlw 1sdh 1cgi 1xnf/; my $re = join "|", map quotemeta, @patterns; my %seen; /($re)/ and $seen{$1}++ for (@data); my $unique = join "|", map quotemeta, grep { $seen{$_} == 1 } @pattern +s; my @unique = grep { /$unique/ } @data;

— Arien

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2025-02-09 02:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which URL do you most often use to access this site?












    Results (95 votes). Check out past polls.