I want to write a Scala script to recursively process all files in a directory. For each file I’d like to see if there are any cases where a string occurs at line X and line X – 2. If a case like that occurs I’d like to stop processing that file, and add that filename to a map of filenames to occurrence counts. I just started learning Scala today, I’ve got the file recurse code working, and need some help with the string searching, here’s what I have so far:
import java.io.File
import scala.io.Source
val s1= "CmdNum = 506"
val s2 = "Data = [0000,]"
def processFile(f: File) {
val lines = scala.io.Source.fromFile(f).getLines.toArray
for (i = 0 to lines.length - 1) {
// want to do string searches here, see if line contains s1 and line two lines above also contains s1
//println(lines(i))
}
}
def recursiveListFiles(f: File): Array[File] = {
val these = f.listFiles
if (these != null) {
for (i = 0 to these.length - 1) {
if (these(i).isFile) {
processFile(these(i))
}
}
these ++ these.filter(_.isDirectory).flatMap(recursiveListFiles)
}
else {
Array[File]()
}
}
println(recursiveListFiles(new File(args(0))))
You can do something like this:
First you don’t need to convert to an array, you can preserve the iterator to only read the lines necessary to find a match.
You can use
slidingto get a derived iterator using a sliding window of 3 lines where you look for the string on lineiandi+2.existstests whether an element of this sliding iterator satisfy a predicate. Thecasewill pattern match the 3 lines from the sliding window element into 3 vals for convenience. I had to use the REPL to find out what type the sliding was really returning.Finally don’t forget to close src.
If you need the occurrence count:
You filter the occurrences and then get the size…
edited for match error on files shorter than 3 lines