Hi PerlMonks,
I have a script here that need some tweaking to achieve the outcome that I am after:
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:
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
When I run the script, what I am getting is:
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
-
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.