scala ..将消息解析到各个字段中

时间:2016-09-23 14:18:32

标签: scala layout

在Scala中,我想将每条消息(长度= 20)解析为单个单元。该消息将附加到上一条消息的末尾,而不会出现换行符。我尝试了以下内容,但欢迎任何优化和改进性能

/* Length.. id=3,name=5,city=8,port=3,indicator=1 */

def layout(rec:String) = { 
val id=rec.take(3)
val name=rec.drop(3).take(5)
val city=rec.drop(3+5).take(8)
val port=rec.drop(3+5+8).take(3)
val ind=rec.drop(3+5+8+3).take(1)
println(id,name,city,port,ind) 
}

val messages="101Jim  Portland990Y102JamesHouston 990X103John Boston  880Y" 
messages grouped(20)  foreach { x => layout(x) } 


In REPL, 

scala> :load work.scala
Loading work.scala...
layout: (rec: String)Unit
messages: String = 101Jim  Portland990Y102JamesHouston 990X103John Boston  880Y
(101,Jim  ,Portland,990,Y)
(102,James,Houston ,990,X)
(103,John ,Boston  ,880,Y)

scala>

1 个答案:

答案 0 :(得分:5)

你可以用正则表达式很好地完成这个任务:

val messages = "101Jim  Portland990Y102JamesHouston 990X103John Boston  880Y"

val RecordPattern = """(\d{3})(.{5})(.{8})(\d{3})(.)""".r

val records = messages.grouped(20).map {
  case RecordPattern(id, name, city, port, ind) => (id, name, city, port, ind)
}

然后:

scala> records.foreach(println)
(101,Jim  ,Portland,990,Y)
(102,James,Houston ,990,X)
(103,John ,Boston  ,880,Y)

这也可能比使用droptake之类的集合操作分割字符串更好,但差别很小,主要优点是清晰度。