早上好。
我在Spark中使用Scala已经有几个月了,我被困在一个for -循环中,给了我一个空(即Unit)内容。我想知道你能不能帮帮忙。以下是使用的代码行:
val slr = lsresult.split(Array(' ', '\n')).filter(_.startsWith("/data")) // Extraction of File Path starting with "/data"
val slrMap = slr.map(x => 0.until(x.length).filter(x.startsWith("/",_))).map(x => x.slice(5,7)).map{case Vector(a,b)=>(a,b).toString} // Extraction of the position of the third party data collection agency (i.e., sourcename) of the file
// Knowing what is the positioning of the "/" character in the filename help in determining the sourcename. Here, string between 6th and 7th "/" contains the agency's name hence "slice (5,7)"
val charStart= slrMap.map(_.slice(1,3).toInt)
val charStop= slrMap.map(_.slice(4,6).toInt)以下是返回单元的方法
def sourcename = {for (a<-Range(0,slr.length)) {print(slr{a}.slice(charStart{a}, charStop{a}))};}在执行最后一行时,我得到
原始名称:单位
但是,当我随后输入sourcename时,它确实产生了我想要的结果(即显示所需的源名),但仍然将其视为阻止我将其转换为字符串的Unit。
非常感谢你在这方面的帮助。
致以敬意,
克里斯蒂安
发布于 2016-09-19 10:30:27
你忘了在结尾写yield
for-理解语法
通常情况下,for comprehension看起来是这样的
for {
a <- something
b <- something if a > 10
} yield b在你的情况下
for {
a <- 0 to slr.length
} yield slr(a).slice(charStart(a), charStop(a))https://stackoverflow.com/questions/39570911
复制相似问题