Files and Text Streams
Parse Records
Parse short record text and total a numeric field.
parse-records
Text records can be parsed with small, bounded loops.
Parse Records
ParseRecords.kt
Replay: real traced execution (multi-file project)
fun main() {
val records = "tea:2;rice:3"
val bonus = 0
val rows = records.split(";")
var total = bonus
var names = ""
for (row in rows) {
val fields = row.split(":")
val name = fields[0]
val count = fields[1].toInt()
total += count
names += name
}
println("bonus=$bonus")
println("rows=${rows.size}")
println("total=$total")
println("names=$names")
}
fun main() {
val records = "tea:2;rice:3"
val bonus = 1
val rows = records.split(";")
var total = bonus
var names = ""
for (row in rows) {
val fields = row.split(":")
val name = fields[0]
val count = fields[1].toInt()
total += count
names += name
}
println("bonus=$bonus")
println("rows=${rows.size}")
println("total=$total")
println("names=$names")
}
fun main() {
val records = "tea:2;rice:3"
val bonus = 5
val rows = records.split(";")
var total = bonus
var names = ""
for (row in rows) {
val fields = row.split(":")
val name = fields[0]
val count = fields[1].toInt()
total += count
names += name
}
println("bonus=$bonus")
println("rows=${rows.size}")
println("total=$total")
println("names=$names")
}
records ← tea:2;rice:3, bonus ← 0, rows ← [tea:2, rice:3], total ← 0
1fun main() {2 val records→ tea:2;rice:3 = "tea:2;rice:3"3 val bonus→ 0 = 0 //@bonus=1, 54 val rows→ [tea:2, rice:3] = recordstea:2;rice:3.split(";")5 var total→ 0 = bonus06 var names→ (empty) = ""fields ← [tea, 2], name ← tea, count ← 2, total ← 2, names ← tea
pass 1 of 28for (rowtea:2 in rows[tea:2, rice:3]) {9 val fields→ [tea, 2] = rowtea:2.split(":")10 val name→ tea = fields[0]tea11 val count→ 2 = fields[1]2.toInt()12 total→ 2 += count213 names→ tea += nametea14}fields ← [rice, 3], name ← rice, count ← 3, total ← 5, names ← tearice
pass 2 of 28for (rowrice:3 in rows[tea:2, rice:3]) {9 val fields→ [rice, 3] = rowrice:3.split(":")10 val name→ rice = fields[0]rice11 val count→ 3 = fields[1]3.toInt()12 total→ 5 += count313 names→ tearice += namerice14}println("bonus=$bonus")
16 println("bonus=$bonus0")17 println("rows=${rows.size2}")18 println("total=$total5")19 println("names=$namestearice")20}outputbonus=0 rows=2 total=5 names=tearicevalues this step[tea:2, rice:3]rows
records ← tea:2;rice:3, bonus ← 1, rows ← [tea:2, rice:3], total ← 1
1fun main() {2 val records→ tea:2;rice:3 = "tea:2;rice:3"3 val bonus→ 1 = 14 val rows→ [tea:2, rice:3] = recordstea:2;rice:3.split(";")5 var total→ 1 = bonus16 var names→ (empty) = ""fields ← [tea, 2], name ← tea, count ← 2, total ← 3, names ← tea
pass 1 of 28for (rowtea:2 in rows[tea:2, rice:3]) {9 val fields→ [tea, 2] = rowtea:2.split(":")10 val name→ tea = fields[0]tea11 val count→ 2 = fields[1]2.toInt()12 total→ 3 += count213 names→ tea += nametea14}fields ← [rice, 3], name ← rice, count ← 3, total ← 6, names ← tearice
pass 2 of 28for (rowrice:3 in rows[tea:2, rice:3]) {9 val fields→ [rice, 3] = rowrice:3.split(":")10 val name→ rice = fields[0]rice11 val count→ 3 = fields[1]3.toInt()12 total→ 6 += count313 names→ tearice += namerice14}println("bonus=$bonus")
16 println("bonus=$bonus1")17 println("rows=${rows.size2}")18 println("total=$total6")19 println("names=$namestearice")20}outputbonus=1 rows=2 total=6 names=tearicevalues this step[tea:2, rice:3]rows
records ← tea:2;rice:3, bonus ← 5, rows ← [tea:2, rice:3], total ← 5
1fun main() {2 val records→ tea:2;rice:3 = "tea:2;rice:3"3 val bonus→ 5 = 54 val rows→ [tea:2, rice:3] = recordstea:2;rice:3.split(";")5 var total→ 5 = bonus56 var names→ (empty) = ""fields ← [tea, 2], name ← tea, count ← 2, total ← 7, names ← tea
pass 1 of 28for (rowtea:2 in rows[tea:2, rice:3]) {9 val fields→ [tea, 2] = rowtea:2.split(":")10 val name→ tea = fields[0]tea11 val count→ 2 = fields[1]2.toInt()12 total→ 7 += count213 names→ tea += nametea14}fields ← [rice, 3], name ← rice, count ← 3, total ← 10, names ← tearice
pass 2 of 28for (rowrice:3 in rows[tea:2, rice:3]) {9 val fields→ [rice, 3] = rowrice:3.split(":")10 val name→ rice = fields[0]rice11 val count→ 3 = fields[1]3.toInt()12 total→ 10 += count313 names→ tearice += namerice14}println("bonus=$bonus")
16 println("bonus=$bonus5")17 println("rows=${rows.size2}")18 println("total=$total10")19 println("names=$namestearice")20}outputbonus=5 rows=2 total=10 names=tearicevalues this step[tea:2, rice:3]rows