Here's another tybalt89-ish solution:
c:\@Work\Perl\monks>perl -wMstrict -le
"use Test::More 'no_plan';
use Test::NoWarnings;
;;
use constant W => 0.5;
;;
my @records = (
'U_TOP_LOGIC/ipre_reg_0/Q,0,0,1,1,0,0,1,1,0,0',
'U_TOP_LOGIC/ipre_reg_6/Q,1,1,0,0,1,1,0,0,1,1',
'U_TOP_LOGIC/pre_reck_1/Q,1,1,0,1,1,0,0,1,1,0',
'U_TOP_LOGIC/pre_reg_10/Q,0,1,0,1,1,0,0,1,1,1',
'U_TOP_LOGIC/pre_reg_11/Q,0,0,1,0,1,0,0,1,0,1',
);
;;
my $rx_transition = qr{ (?<= 1) 0 | (?<= 0) 1 }xms;
;;
my @pat = (0);
for my $record (@records) {
my (undef, $trans) = split m{ \d/Q, }xms, $record, 2;
$trans =~ tr/01//cd;
$pat[ $-[0] ]++ while $trans =~ m{ $rx_transition }xmsg;
}
$_ *= W for @pat;
;;
is_deeply \@pat, [ 0, 0.5, 2.5, 1.5, 1.5, 1.5, 1, 1.5, 1.5, 1, ],
'weighted';
;;
done_testing;
"
ok 1 - weighted
1..1
ok 2 - no warnings
1..2
Update: You can simplify the code slightly by changing the
$pat[ $-[0] ]++ while $trans =~ m{ $rx_transition }xmsg;
statement to
$pat[ $-[0] ] += W while $trans =~ m{ $rx_transition }xmsg;
and getting rid of the
$_ *= W for @pat;
post-loop weighting fixup statement altogether – but this code might be slightly slower.
Give a man a fish: <%-{-{-{-<
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.