monkfan has asked for the wisdom of the Perl Monks concerning the following question:
Currently the answers it gives are:#!/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; }
The desired answers for "BA" areSubstring 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**
Substring to Count: BA 1 - ABCA 0 - ACDB 0 - ABIC - CADH Total Support: 1
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Counting Array in an Array of HoA
by Hena (Friar) on May 09, 2005 at 09:06 UTC | |
by monkfan (Curate) on May 15, 2005 at 10:06 UTC | |
|
Re: Counting Array in an Array of HoA
by reneeb (Chaplain) on May 09, 2005 at 09:22 UTC | |
by monkfan (Curate) on May 09, 2005 at 14:13 UTC | |
by omega_monk (Scribe) on May 09, 2005 at 16:03 UTC |