所以这是我的代码片段:
val check = collection.mutable.Set[String]()
if (check.isEmpty) {
println("There is no transfer data available yet, please use the 'load' command to initialize the application!")
}
val userSelection = scala.io.StdIn.readLine("Enter your command or type 'help' for more information:")
val loadCommand = """^(?:load)\s+(.*)$""".r
userSelection match {
case loadCommand(fileName) => check add fileName ;print(check);val l= loadOperation(fileName);println(l.length); if (check.contains(fileName)){
programSelector()
}else{ loadOperation(fileName)}
首先,我有一个输入匹配,一个案例是“加载”输入。 现在我的程序能够加载不同的.csv文件并将其存储到List中。但是,只要它加载一个新的.csv文件来检查它是否先前已加载,我想要我的程序。如果它之前已加载,则不应加载两次!! 所以我认为它可以使用Set检查,但它总是覆盖我以前的fileName条目...
答案 0 :(得分:1)
您的set
操作没问题。问题是check add fileName
case loadCommand(fileName)
应该在else
块之后。 (我们现在和那之后的那些“d'时刻之一!?):)
val check = collection.mutable.Set[String]()
def process(userSelection: String): String = {
val loadCommand = """^(?:load)\s+(.*)$""".r
userSelection match {
case loadCommand(fileName) =>
if (check.contains(fileName))
s"Already processed $fileName"
else {
check add fileName
s"Process $fileName"
}
case _ =>
"User selection not recognised"
}
}
process("load 2013Q1.csv")
process("load 2013Q2.csv")
process("load 2013Q1.csv")
res1: String = Process 2013Q1.csv
res2: String = Process 2013Q2.csv
res3: String = Already processed 2013Q1.csv