ezoic

Wednesday, June 14, 2017

foreach loop can not update the list in scala, but for loop can

I did a script, part of it is to update a list within a loop. foreach loop can not update the list, but for loop can. Here is the sample code:

For loop:

var Typea = scala.collection.mutable.ListBuffer[String]()
var Typeb =  scala.collection.mutable.ListBuffer[String]()



val Pattern1 = "TYPEA".r

val Pattern2 = "TYPEB".r
var rrr=rdd.toDF.collect()
for (f<-rrr) {
      val fName=f.getString(0)
     for (k<- fName.split("\\/")) { if ( Pattern1.findFirstIn(k)!=None)
{println(k);  Typea:+=k.toString}
else if ( Pattern2.findFirstIn(k)!=None)
{println(k); Typeb:+=k.toString}

}
}

val Typea1=Typea.distinct.sorted
val Typeb1=Typeb.distinct.sorted
println(Typea)
println(Typeb)

println(Typea1)
println(Typeb1)

It updated the list, Typea, Typeb. After updating, I printed the lists, there were  strings in it.



Foreach loop:

var Typea = scala.collection.mutable.ListBuffer[String]()
var Typeb =  scala.collection.mutable.ListBuffer[String]()



val Pattern1 = "TYPEA".r

val Pattern2 = "TYPEB".r
rdd.toDF.foreach{ f=>
      val fName=f.getString(0)
      fName.split("\\/").foreach(k=> if ( Pattern1.findFirstIn(k)!=None)
{println(k);  Typea:+=k.toString; println(Typea)}
else if ( Pattern2.findFirstIn(k)!=None)
{println(k); Typeb:+=k.toString;println(Typeb)}
)
}

val Typea1=Typea.distinct.sorted
val Typeb1=Typeb.distinct.sorted
println(Typea)
println(Typeb)

println(Typea1)
println(Typeb1)

After updating the list, I printed the lists, they were empty.

No comments:

Post a Comment

looking for a man

 I am a mid aged woman.  I was born in 1980. I do not have any kid. no complicated  dating before . I am looking for a man here for marriage...