Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Perl script for the post processing of one CSV file

by AnomalousMonk (Archbishop)
on Oct 03, 2019 at 17:54 UTC ( #11107017=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11107017]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2023-09-30 22:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?