troubleshooting Question

Perl Script to count the number of elements in an array

Avatar of Jason_Sutiono
Jason_Sutiono asked on
ProgrammingPerl
5 Comments1 Solution411 ViewsLast Modified:
Hi all,

Would really appreciate some input on how to do the following  with a Perl script to process a text file.

Here is my 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

The final output that I am trying to achieve:
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

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($col[12] != " "), i need to count the number of string in the last column that appeared after it.
W035|S|
W035|S|08961029909655092918
W035|S|08961029909655092926
W035|S|08961029909655092934
W035|S|08961029909655092942

As such, the outcome for line 1 would be:
BLA|001036|S|3228|10|1|2|3|001036|W035|S| |4

In other words, $lastcol(001036)(3228)=4

The count of the strings is appended to the last column.

I would also require col 5,6,7,8 from line 1.

Likewise for 123777 and 3327, since there are 2 strings that appear in the entries below it (08961029909655093791 and 08961029909655093775), the outcome is
CHL|123777|S|3327|3.0000|0.0000|0.0000|0.0000|001036|W035|S| |2

If there are no entries below it, I would just append a 0 at the end of it
e.g BLT|600123|S|3437|0|20|0|0|001036|W035|S| |0

I hope I am clear in my brief.

Looking forward to the responses!!

Thank you in advance!

Jason
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros