Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: list of unique strings, also eliminating matching substrings

by wind (Priest)
on May 21, 2011 at 03:32 UTC ( [id://906030]=note: print w/replies, xml ) Need Help??


in reply to list of unique strings, also eliminating matching substrings

First method, an old fashioned loop within a loop.
my @list = qw(AGCT AGGT GG AGCT); MAIN: for my $i (0..$#list) { my $substr_re = qr/$list[$i]/; for my $j (0..$#list) { next if $i == $j || ! defined $list[$j]; if ($list[$j] =~ $substr_re) { undef $list[$i]; next MAIN; } } } my @unique = grep {defined} @list; print "$_\n" for @unique;

Update: Increase efficiency by grouping the strings by size before processing:

use strict; use warnings; my @list = qw(AGCT AGGT GG AGCT); my %bucket; for (@list) { push @{$bucket{length($_)}}, $_; } # Only want to sort these once. my @sizes = sort {$a <=> $b} keys %bucket; while (my $size = shift @sizes) { MAIN: for my $i (0..$#{$bucket{$size}}) { # Same Size first for my $j ($i+1..$#{$bucket{$size}}) { if ($bucket{$size}[$i] eq $bucket{$size}[$j]) { undef $bucket{$size}[$i]; next MAIN; } } # Bigger strings my $substr_re = qr/$bucket{$size}[$i]/; for my $bigger (@sizes) { for my $str (@{$bucket{$bigger}}) { if ($str =~ $substr_re) { undef $bucket{$size}[$i]; next MAIN; } } } } } my @unique = grep {defined} map {@$_} values %bucket; print "$_\n" for @unique;

Replies are listed 'Best First'.
Re^2: list of unique strings, also eliminating matching substrings
by lindsay_grey (Novice) on May 21, 2011 at 03:36 UTC
    I was thinking that might be necessary but hoping not. Thank you for posting your code.
Re^2: list of unique strings, also eliminating matching substrings
by lindsay_grey (Novice) on May 21, 2011 at 04:07 UTC
    Thank you! I will definitely try this too.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-20 10:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found