I have a text file that contains lines that follow an alternate pattern, like:
name: SomeName
counterA: 0, counterB: 0, counterC: 0
name: SomeNameB
counterA: 1, counterB: 2, counterC: 3
...
I want to write a simple parser that pushes SameName, and the counters A to C into a new object.
So the task is basically to process always two lines together.
I started with this code:
fun readFileAsLinesUsingReadLines(fileName: String): List<String> = File(fileName).readLines()
fun main(args: Array<String>) {
val lines = readFileAsLinesUsingReadLines("Whatever")
for (i in (0 .. lines.size-1 step 2)) {
println(lines[i]+lines[i+1])
}
}
and yes, that prints
name: SomeName counterA: 0, counterB: 0, counterC: 0
which I can then further parse.
But I find that usage of (0 .. lines-size-1 step) to be not very elegant.
Are there more elegant, or "more kotlin" ways of pulling that information?