Aman
Aman

Reputation: 3261

Count filtered records in scala

As I am new to scala ,This problem might look very basic to all..
I have a file called data.txt which contains like below:

xxx.lss.yyy23.com-->mailuogwprd23.lss.com,Hub,12689,14.98904563,1549
xxx.lss.yyy33.com-->mailusrhubprd33.lss.com,Outbound,72996,1.673717588,1949
xxx.lss.yyy33.com-->mailuogwprd33.lss.com,Hub,12133,14.9381027,664
xxx.lss.yyy53.com-->mailusrhubprd53.lss.com,Outbound,72996,1.673717588,3071

I want to split the line and find the records depending upon the numbers in xxx.lss.yyy23.com

 val data = io.Source.fromFile("data.txt").getLines().map { x => (x.split("-->"))}.map { r => r(0) }.mkString("\n")  

which gives me

xxx.lss.yyy23.com
xxx.lss.yyy33.com
xxx.lss.yyy33.com
xxx.lss.yyy53.com  

This is what I am trying to count the exact value...

 data.count { x => x.contains("33")}  

How do I get the count of records who does not contain 33...

Upvotes: 4

Views: 2262

Answers (1)

DemetriKots
DemetriKots

Reputation: 1224

The following will give you the number of lines that contain "33":

data.split("\n").count(a => a.contains("33"))

The reason what you have above isn't working is that you need to split data into an array of strings again. Your previous statement actually concatenates the result into a single string using newline as a separator using mkstring, so you can't really run collection operations like count on it.

The following will work for getting the lines that do not contain "33":

data.split("\n").count(a => !a.contains("33"))

You simply need to negate the contains operation in this case.

Upvotes: 5

Related Questions