quelos27 has asked for the wisdom of the Perl Monks concerning the following question:
my %c; while( <> ){ chomp; my($c2,$c4,$c12)=(split/\|/)[1,3,11]; $c{"$c2|$c4"}||=[$_,0]; ++$c{"$c2|$c4"}[1] if $c12=~/\S/; } $"="|"; for( sort keys %c ){ print "@{$c{$_}}\n"; }
Here is the input file:
col1|col2|col3|col4|col5|col6|col7|col8|col9|col10|col11|col12 BLA|001036|S|3228|10|1|2|3|001036|W035|S| BLA|001036|S|3228|0|0|0|0|001036|W035|S|08961029909655092918 BLA|001036|S|3228|0|0|0|0|001036|W035|S|08961029909655092926 BLA|001036|S|3228|0|0|0|0|001036|W035|S|08961029909655092934 BLA|001036|S|3228|0|0|0|0|001036|W035|S|08961029909655092942 BLT|600123|S|3437|0|20|0|0|001036|W035|S| BRO|900177|S|3531|-1|0|0|0|001036|W035|S| CHL|123777|S|3327|3|0|0|0|001036|W035|S| CHL|123777|S|3327|0|0|0|0|001036|W035|S|08961029909655093791 CHL|123777|S|3327|0|0|0|0|001036|W035|S|08961029909655093775
Basically I am trying to count the number of string that appears in the last column and append the count as a new column in the output file. My references/main keys for the initial array are column 2 (001036) and column 4 (3228). For each new occurrence of col 2 and col 4(e.g 001036 and 3228), the last column would always be a space (" "). So if($col12 != " "), i need to count the number of string in the last column that appeared after it. The final outcome should look something like the following:
When I run the script, what I am getting is:BLA|001036|S|3228|10|1|2|3|001036|W035|S| |4 BLT|600123|S|3437|0|20|0|0|001036|W035|S| |0 BRO|900177|S|3531|-1|0|0|0|001036|W035|S| |0 CHL|123777|S|3327|3|0|0|0|001036|W035|S| |2
BLA|001036|S|3228|10|1|2|3|001036|W035|S| |4 CHL|123777|S|3327|3|0|0|0|001036|W035|S| |2 BLT|600123|S|3437|0|20|0|0|001036|W035|S| |0 BRO|900177|S|3531|-1|0|0|0|001036|W035|S| |0
How do I get the count value to not print to a new line? In desperate need of help. I am fairly new with perl. Thank you in advance!! Regards, Jason
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Need help with a simple perl script
by Neighbour (Friar) on May 23, 2012 at 08:45 UTC | |
Re: Need help with a simple perl script
by choroba (Cardinal) on May 23, 2012 at 08:46 UTC | |
Re: Need help with a simple perl script
by kcott (Archbishop) on May 23, 2012 at 17:30 UTC | |
Re: Need help with a simple perl script
by live4tech (Sexton) on May 24, 2012 at 04:24 UTC | |
Re: Need help with a simple perl script
by uday_sagar (Scribe) on May 23, 2012 at 10:04 UTC | |
by choroba (Cardinal) on May 23, 2012 at 12:07 UTC | |
by uday_sagar (Scribe) on May 24, 2012 at 11:41 UTC | |
by choroba (Cardinal) on May 24, 2012 at 11:56 UTC |