Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Hmmm ... I thought there would be more activity on this thread. No-one seems to be actively working on it, so here's the code I used to get my timings.

#!/usr/bin/perl # # multi-string-match.pl <FName> # # Grind through a set of strings, and keep only the ones that don't + contain # any of the others as a substring. FName is a file containing a l +ist of # strings, and if null, we'll use our test data. # # Inspired by perlmonks node 906020, and the Knuth-Morris-Pratt alg +orithm. # use strict; use warnings; use feature ':5.10'; # function is 10.67 chars wide, so need to round up, or we can't find +partials # (previous state will linger, so we can't find 'em!) my $hashwidth = 11; # our alphabet my %xlat = (A=>1, C=>2, G=>3, T=>4, N=>0); my @unique; my @candidates; my %MatchKeys; my $fname = shift; open my $FH, '<', $fname or die; @candidates = <$FH>; @candidates = grep { /^[ACGTN]+$/ } # delete the comments map { s/^\s+//; s/\s+$//; $_ } @candidates; my $start = time; @candidates = sort { length($a) <=> length($b) || $a cmp $b } @candida +tes; my (@keypath, $t); #, @chars, @keypath); my $cnt_dup=0; CANDIDATE: while ($t = shift @candidates) { my $h = 0; my $keywidth=0; @keypath=(); my $rMatchKeys = \%MatchKeys; my $fl_partial=-1; my $l = length($t); while ($keywidth < $l) { $h = hash(substr($t,$keywidth,1), $h); ++$keywidth; if ($keywidth % $hashwidth == 0) { push @keypath, $h; } if ($fl_partial < 0) { # No current partial match if (exists $MatchKeys{$h}) { $rMatchKeys = $$rMatchKeys{$h}; $fl_partial = $keywidth; } } else { if ( ($keywidth - $fl_partial) % $hashwidth == 0 ) { $rMatchKeys = exists($$rMatchKeys{$h}) ? $$rMatchKeys{ +$h} : undef; } elsif (exists($$rMatchKeys{REM}) and exists($$rMatchKeys{R +EM}{$h})) { ++$cnt_dup; next CANDIDATE; } } } my $ar = [ $h, $keywidth % $hashwidth ]; ### Add the path to %MatchKeys $rMatchKeys = \%MatchKeys; while (my $r = shift @keypath) { $$rMatchKeys{$r} = { } if !exists $$rMatchKeys{$r}; $rMatchKeys = $$rMatchKeys{$r}; } $$rMatchKeys{REM} = { } if !exists $$rMatchKeys{REM}; if (exists($$rMatchKeys{REM}{$$ar[0]}) and $$ar[1] == $$rMatchKeys{REM}{$$ar[0]}) { ++$cnt_dup; next CANDIDATE; } $$rMatchKeys{REM}{$$ar[0]} = $$ar[1]; push @unique, $t; } my $end = time - $start; print scalar(@unique), " unique items\n"; print "$cnt_dup rejected.\n"; print "$end seconds.\n"; sub hash { my ($curchar, $prevhash) = @_; $prevhash = ($prevhash * 8 + $xlat{$curchar}) & 0xffffffff; }

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re^2: list of unique strings, also eliminating matching substrings by roboticus
in thread list of unique strings, also eliminating matching substrings by lindsay_grey

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (2)
    As of 2014-09-21 11:38 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (168 votes), past polls