Jason_Sutiono
asked on
Perl Script to count the number of elements in an array
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|c ol6|col7|c ol8|col9|c ol10|col11 |col12
BLA|001036|S|3228|10|1|2|3 |001036|W0 35|S|
BLA|001036|S|3228|0|0|0|0| 001036|W03 5|S|089610 2990965509 2918
BLA|001036|S|3228|0|0|0|0| 001036|W03 5|S|089610 2990965509 2926
BLA|001036|S|3228|0|0|0|0| 001036|W03 5|S|089610 2990965509 2934
BLA|001036|S|3228|0|0|0|0| 001036|W03 5|S|089610 2990965509 2942
BLT|600123|S|3437|0|20|0|0 |001036|W0 35|S|
BRO|900177|S|3531|-1|0|0|0 |001036|W0 35|S|
CHL|123777|S|3327|3|0|0|0| 001036|W03 5|S|
CHL|123777|S|3327|0|0|0|0| 001036|W03 5|S|089610 2990965509 3791
CHL|123777|S|3327|0|0|0|0| 001036|W03 5|S|089610 2990965509 3775
The final output that I am trying to achieve:
BLA|001036|S|3228|10|1|2|3 |001036|W0 35|S| |4
BLT|600123|S|3437|0|20|0|0 |001036|W0 35|S| |0
BRO|900177|S|3531|-1|0|0|0 |001036|W0 35|S| |0
CHL|123777|S|3327|3|0|0|0| 001036|W03 5|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|0896102990965509291 8
W035|S|0896102990965509292 6
W035|S|0896102990965509293 4
W035|S|0896102990965509294 2
As such, the outcome for line 1 would be:
BLA|001036|S|3228|10|1|2|3 |001036|W0 35|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.00 00|0.0000| 001036|W03 5|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|W0 35|S| |0
I hope I am clear in my brief.
Looking forward to the responses!!
Thank you in advance!
Jason
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|c
BLA|001036|S|3228|10|1|2|3
BLA|001036|S|3228|0|0|0|0|
BLA|001036|S|3228|0|0|0|0|
BLA|001036|S|3228|0|0|0|0|
BLA|001036|S|3228|0|0|0|0|
BLT|600123|S|3437|0|20|0|0
BRO|900177|S|3531|-1|0|0|0
CHL|123777|S|3327|3|0|0|0|
CHL|123777|S|3327|0|0|0|0|
CHL|123777|S|3327|0|0|0|0|
The final output that I am trying to achieve:
BLA|001036|S|3228|10|1|2|3
BLT|600123|S|3437|0|20|0|0
BRO|900177|S|3531|-1|0|0|0
CHL|123777|S|3327|3|0|0|0|
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|0896102990965509291
W035|S|0896102990965509292
W035|S|0896102990965509293
W035|S|0896102990965509294
As such, the outcome for line 1 would be:
BLA|001036|S|3228|10|1|2|3
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
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
I hope I am clear in my brief.
Looking forward to the responses!!
Thank you in advance!
Jason
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@soh = grep /\|\s*$/,<FILE4>;
ASKER
Hi Ozo,
Thank you for your help!!
Its almost there just one thing though. The output that I get is:
BLA|001036|S|3228|10|1|2|3 |001036|W0 35|S|
|4
CHL|123777|S|3327|3|0|0|0| 001036|W03 5|S|
|2
BLT|600123|S|3437|0|20|0|0 |001036|W0 35|S|
|0
BRO|900177|S|3531|-1|0|0|0 |001036|W0 35|S|
|0
How do I get the count value to not print to a new line?
BLA|001036|S|3228|10|1|2|3 |001036|W0 35|S| |4
CHL|123777|S|3327|3|0|0|0| 001036|W03 5|S| |2
BLT|600123|S|3437|0|20|0|0 |001036|W0 35|S| |0
BRO|900177|S|3531|-1|0|0|0 |001036|W0 35|S| |0
Thank you in advance!!
Thank you for your help!!
Its almost there just one thing though. The output that I get is:
BLA|001036|S|3228|10|1|2|3
|4
CHL|123777|S|3327|3|0|0|0|
|2
BLT|600123|S|3437|0|20|0|0
|0
BRO|900177|S|3531|-1|0|0|0
|0
How do I get the count value to not print to a new line?
BLA|001036|S|3228|10|1|2|3
CHL|123777|S|3327|3|0|0|0|
BLT|600123|S|3437|0|20|0|0
BRO|900177|S|3531|-1|0|0|0
Thank you in advance!!
ASKER
Thanks Ozo u rock!!
ASKER
Actually there is one thing I forgot to ask.
Input:
col1|col2|col3|col4|col5|c
MCA|U8350WHT|S|3320|1|0|0|
MCA|U8350WHT|S|3320|0|0|0|
MEL|U8350WHT|S|3532|2|0|0|
MEL|U8350WHT|S|3532|0|0|0|
MEL|U8350WHT|S|3532|0|0|0|
MOR|U8350WHT|S|3867|1|0|0|
MOR|U8350WHT|S|3867|0|0|0|
PEN|U8350WHT|S|3526|1|0|0|
PEN|U8350WHT|S|3526|0|0|0|
What should I do to get only rows where the last column is equals to " "?
Outcome:
MCA|U8350WHT|S|3320|1|0|0|
MEL|U8350WHT|S|3532|2|0|0|
MOR|U8350WHT|S|3867|1|0|0|
PEN|U8350WHT|S|3526|1|0|0|
I have tried to only filter by $col12=" " as per attached but it actually prints out everything without excluding those that are not " ".
Help would be much appreciated.
Thank you!
Open in new window