For example

Update: key value look-up was wrong - fixed

use strict; use warnings; use Smart::Comments '###'; use Math::Fleximal; my @observed = ("ab", "ab", "ad", "an", "bd", "bn", "dn"); my $ngramOrder = 2; my $firstletter = 'a'; my $lastletter = 'z'; my $flexref = [ $firstletter..$lastletter ]; my $count_ref; my $base = new Math::Fleximal( 'a', $flexref); my $position = new Math::Fleximal( 'zz', $flexref); my $base_10 = new Math::Fleximal( 0, [ 0..9 ] ); ### Fleximal Range is: $position->subtr( $base )->base_10 ### Fleximals count from 0! $| = 1; ### Build the list of hits for my $item( @observed ){ if( length( $item ) == $ngramOrder and $item =~/^[a-z]*$/ ){ #Add other scubbers as needed $count_ref->{$item}++;#Auto vivifys new keys } } ### Result: $count_ref ### Access the possibilities ### Print known counts (counts from zero!) for my $key ( sort keys %$count_ref ) { $position = $position->set_value( $key ); my $int_position = $position->subtr( $base )->base_10; print "At position -$int_position- the count is: $count_ref->{$key +}\n"; #~ ### $position } ### lookup a position count my $number_position = 50; print "At position -$number_position- the count is: " . count_at_position( $number_position ) . "\n"; $number_position = 1; print "At position -$number_position- the count is: " . count_at_position( $number_position ) . "\n"; sub count_at_position{ my ( $integer ) = @_; my $flex_int = $base_10->set_value( $integer ); my $key = $flex_int->change_flex( $flexref ); my $string = $key->to_str(); while( length( $string ) != $ngramOrder ) { $string = $firstletter . $string; } ### $string my $count = ( exists $count_ref->{$string} ) ? $count_ref->{$string} : 0 ; return $count; }
Gives results
### Fleximal Range is: '675' ### Fleximals count from 0! ### Build the list of hits ### Result: { ### ab => 2, ### ad => 1, ### an => 1, ### bd => 1, ### bn => 1, ### dn => 1 ### } ### Access the possibilities ### Print known counts (counts from zero!) At position -1- the count is: 2 At position -3- the count is: 1 At position -13- the count is: 1 At position -29- the count is: 1 At position -39- the count is: 1 At position -91- the count is: 1 ### lookup a position count ### $string: 'by' At position -50- the count is: 0 ### $string: 'ab' At position -1- the count is: 2

In reply to Re^4: counting instances of one array in another array by jandrew
in thread 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.