perl - Splitting and tallying substrings within mixed integer-string data -


input data (example):

40a3b35a3c 30a5b28a2c2b 

desired output (per-line) single number determined composition of code 40a3b35a3c , following rules:

if - add proceeding number running total if b - add proceeding number running total if c - subtract proceeding number running total 

40a 3b 35a 3c produce 40 + 3 + 35 - 3 = 75.

output both lines:

75 63 

is there efficient way achieve particular column (such $f[2]) in tab-delimited .txt file using one-liner? have considered splitting entire code individual characters, performing if statement checks detect a/b/c, perl knowledge limited , unsure how go this.

when use split capture, captured group returned split, too.

perl -lane '     @ar = split /([abc])/, $f[2];     $s = 0;     $s += $n * ("c" eq $op ? -1 : 1) while ($n, $op) = splice @ar, 0, 2;     print $s ' < input 

or maybe more declarative:

begin { %one = ( =>  1,                  b =>  1,                  c => -1 ) } @ar = split /([abc])/, $f[2]; $s = 0; $s += $n * $one{$op} while ($n, $op) = splice @ar, 0, 2; print $s 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -