in reply to Perl script for the post processing of one CSV file

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:  <%-{-{-{-<