Revered Monks,

Here I am trying to count the support of an array within an array in HoA. A count is considered valid if:
1. Each element of array occur together
2. Order of the array is maintained

Currently this is the solution I have, which I use Regex approach. Yet it still _failed_ to give my desired answer (see answer below). Is there anyway I can solve this problem? Am I right in taking regex approach?
#!/usr/bin/perl -w use strict; use Data::Dumper; # While matching the hash, elem of each array must occur together # and in order my @ar1 = ('A','B'); #Answer: 3 my @ar2 = ('B','A'); #Answer: 1 my @ar3 = ('A','B','C'); #Answer: 2 my $hash = { 'S1' => [ 'A', 'B', 'C', 'A' ], 'S2' => [ 'A', 'C', 'D', 'B' ], 'S3' => [ 'C', 'A', 'D', 'H' ], 'S4' => [ 'A', 'B', 'I', 'C' ] }; print "Total Support: ", count_support_order_based($hash,\@ar1), "\n"; print "Total Support: ", count_support_order_based($hash,\@ar2), "\n"; sub count_support_order_based { my ($hash,$array) = @_; my $tmp_support; my $support; my $total_support; my $array_string = join("", @{$array}); for my $key ( keys %{$hash} ) { my $hash_string = join("",@{$hash->{$key}} ); $tmp_support = () = $hash_string =~ m/[\Q$array_string\E]/g; $support = sprintf("%.0d", $tmp_support/ (length $array_string)); print "$support - $hash_string\n"; if ( $support ) { $total_support += $support; } } # ----- end foreach ----- return $total_support; }
Currently the answers it gives are:
Substring to Count: AB | Substring to Count: BA | 1 - ABCA | 1 - ABCA 1 - ACDB | 1 - ACDB 1 - ABIC | 1 - ABIC - CADH | - CADH Total Support: 3 | Total Support: 3 **CORRECT** **WRONG**
The desired answers for "BA" are
Substring to Count: BA 1 - ABCA 0 - ACDB 0 - ABIC - CADH Total Support: 1
Regards,
Edward

In reply to Counting Array in an Array of HoA by monkfan

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.