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


In reply to Need help with a simple perl script by quelos27

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.