Arrays 如何解析文件,根据需要拆分文件,并将其存储在scala的列表中

Arrays 如何解析文件,根据需要拆分文件,并将其存储在scala的列表中,arrays,list,file,scala,Arrays,List,File,Scala,我是scala的初学者。我现在要做的就是解析一个文件,它的内容有点像这样 You can't legislate morality... but it seems that morons can be legislators. Lottery: A tax on people who are bad at math. These are questions for action, not speculation, which is idle. -- Noam Chomsky If y

我是scala的初学者。我现在要做的就是解析一个文件,它的内容有点像这样

  You can't legislate morality... but it seems that morons can be  legislators.

Lottery: A tax on people who are bad at math.

These are questions for action, not speculation, which is idle.
-- Noam Chomsky

If you think education is expensive try Ignorance.
-- Derek Bok, president of Harvard

Photons have neither morals nor visas.
-- Dave Farber

Maturity is not a factor of the games we play but the occasions we play them!

Design a system an idiot can use and only an idiot will want to use it.

It is much more rewarding to do more with less.
-- Donald Knuth
我一直到现在

import scala.io._


object parseFile {
  var sample : Array[String]= new Array[String](20)
  var anyName = List[Map[String,String]]()
  def main(args:Array[String]):Unit = {
    println("Hello, Scala !! ")
  for(line <- Source.fromFile("myFile.txt").getLines())
    //sample  = line.split("--")
    anyName = Map("quote" -> line):: anyName        
    println(anyName)        

 }
}
导入scala.io_
对象解析文件{
变量示例:数组[字符串]=新数组[字符串](20)
var anyName=List[Map[String,String]]()
def main(参数:数组[字符串]):单位={
println(“你好,Scala!!”)
对于(行)::anyName
println(任意名称)
}
}
列表中的每一行都将与作者姓名一起单独“引用”,作为单独的一行,但我希望列表中的另一个条目为“author”,它应以“-”开头的行为准,并将其拆分

基本上我想分开报价和作者,并保存在一个列表中


提前感谢

我想您不需要地图列表,但可能需要(quote,…,(author,…)类型的元组列表

这可以通过以下方式实现:

import scala.io._

object Quotes {

    def main(args:Array[String]):Unit = {

       val result = Source.fromFile("myFile.txt")     // read file
                          .getLines()                 // line by line
                          .map(_.trim)                // trim spaces on ends
                          .filter(! _.isEmpty)        // ignore empty lines
                          .map { line =>              

              if (line.startsWith("--")) 
                 "author" -> line.drop(2).trim
              else 
                 "quote" -> line
       }

       // result is an iterator of (String, String) tuple.

       println (result.mkString("\n"))

       // if you want a list of such tuples: result.toList
    }
}

/* output looks like this:
(quote,You can't legislate morality... but it seems that morons can be  legislators.)
(quote,Lottery: A tax on people who are bad at math.)
(quote,These are questions for action, not speculation, which is idle.)
(author,Noam Chomsky)
(quote,If you think education is expensive try Ignorance.)
(author,Derek Bok, president of Harvard)
(quote,Photons have neither morals nor visas.)
(author,Dave Farber)
(quote,Maturity is not a factor of the games we play but the occasions we play them!)
(quote,Design a system an idiot can use and only an idiot will want to use it.)
(quote,It is much more rewarding to do more with less.)
(author,Donald Knuth)
*/
用于集合的多次拆分(将文本文件的行拆分为
列表
);每一行都需要这些标准来界定空行和作者的引用

def p(line: String) = {
  val tline = line.trim
  tline.nonEmpty && !tline.startsWith("--")
}
因此

带来一个带有未知作者引号的
列表[List[String]]
(第二项为空),例如

以及引用和归属作者,例如

List("It is much more rewarding to do more with less.", "-- Donald Knuth")
注意:标准适用于文本文件中的(猜测)格式,但
p
可能会调整为其他格式

< >为了产生<代码> map <代码>,从引用到作者,考虑这个

(for ( List(a,b,_*) <- lines ) yield a -> b).toMap
(用于(列表(a,b,*)b)。toMap
List("It is much more rewarding to do more with less.", "-- Donald Knuth")
(for ( List(a,b,_*) <- lines ) yield a -> b).toMap