Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: counting instances of one array in another array

by jsmagnuson (Acolyte)
on Feb 24, 2012 at 11:07 UTC ( [id://955905]=note: print w/replies, xml ) Need Help??


in reply to Re: counting instances of one array in another array
in thread counting instances of one array in another array

Thank you for this approach! This is about 10x faster than the next best approach that was suggested here that I tried implementing (which was this code from an AnonymousMonk):

my %seen; $seen{$_}++ for @observed; @permCounts = map { $seen{$_} || 0 } @perms;

This is good enough to get me going, but I will follow the other advice that was offered and learn about fleximal, PDL::Sparse, and hash-fu. I am very grateful for the generous advice from all of you!

best,

jim

ps -- Slight tweak: abc=(0,1,2), and then 0+(1*26^1)+(2*26^2), etc. (i.e., a=0, z=25, and when string position > 0 you multiply the index of the character in that position by 26 raised to the power of the position).

Replies are listed 'Best First'.
Re^3: counting instances of one array in another array
by MidLifeXis (Monsignor) on Feb 24, 2012 at 13:53 UTC

    Re: your ps - see my ngram2number routine above. It does exactly that calculation.

    Update: after re-reading my original message, I note that you also need to translate a list of characters into an alphabet to use the function (just in case it was not understood from the original code). You can do that with:

    sub charlist2alphabet { my %results; for my $index ( 0 .. (@_ - 1) ) { $results{ $_[ $index ] } = $index; } \%results; } my $alphabet = charlist2alphabet( @charlist );
    or
    my $alphabet = { map { ( $charlist[ $_ ] => $_ ) } ( 0 .. @charlist - +1 );

    --MidLifeXis

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2024-04-18 09:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found