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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |