Filter filtered records in scala

Since I'm new to scala, this problem might look very simple to anyone.
I have a file named data.txt that looks like this:

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 a string and find records based on 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 calculate the exact value ...

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

      

How to get a record counter that does not contain 33 ...

+3


source to share


1 answer


The following is the number of lines containing "33":

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

      

The reason you above doesn't work is because you need to split data

into an array of strings again . Your previous statement is actually concatenating the result into a single line using a newline as a separator, using mkstring

so you cannot run operations like counting.



The following will work for getting lines that don't contain "33":

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

      

In this case, you just need to abandon the addition operation.

+3


source







All Articles