Hello,

I have 2 arrays. One (@perms) has all possible permutations of a set of symbols of a specified length; this is often a very large array. The second array (@observed) is a list of observed ngrams from a string.

What I want to do is create a 3rd array (@permCounts), which has the same length as @perms. I want to initialize this as all zeroes, and then each time I encounter an item from @perms in @observed, I want to increment the corresponding element in @permCounts. Here's how I'm doing it now, but it is very slow when the ngram order is 4 or greater (it begins to take 2+secs per "observed" set, and I have 100s of thousands to evaluate). If anyone can advise me on how to speed this up, I would be most grateful!

use PDL; @observed = ("ab", "ab", "ad", "an", "bd", "bn", "dn"); $ngramOrder = 2; $alph = "{a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z}"; @perms = glob $alph x $ngramOrder; foreach $obs (@observed) { push @hits, grep { $perms[$_] eq $obs } 0..$#perms; } @permCounts=list(zeroes($#perms+1)); foreach $hit (@hits) { $permCounts[$hit]++; }

In reply to counting instances of one array in another array by jsmagnuson

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.